0

I have this Csv file:

DateTime;S1;S2;S3
2020-07-15 16:27:01.221660;652.3826361765446;661.0531016851814;387.24703877989487
2020-07-15 16:27:01.221660;953.4271004457892;873.8955069132078;397.91143086810587

I want to read the file by columns, so:

DateTime_array = [2020-07-15 16:27:01.221660,2020-07-15 16:27:01.221660]
S1_array = [652.3826361765446,953.4271004457892]
S2_array = [661.0531016851814,873.8955069132078]
S3_array = [387.24703877989487,397.91143086810587]

After this I want to plot data, DateTime_array is the X coordinates for each Signal(S1,S2...) and in Signals array are contained the Y coordinates.

How can I do this? Is there any other way to do it straight from the csv?

1
  • 1
    You might need to have a look into pandas.read_csv Commented Jul 16, 2020 at 7:22

2 Answers 2

1

Python's Pandas package is most convenient way to deal with data, including .csv files. You can easily read from you csv:

import pandas as pd
my_df = pd.read_csv(path, parse_dates=True) 

(some adaptations may needed, as .csv has several formats. But this is right method to call)

Now you have a dataframe object looks exactly like your .csv file. It is easy to manipulate data. For example, if you just want to plot one column:

my_df.plot.scatter(x='DataTime', y='S1')

If you want a more complex plot you should use matplotlib package directly.

And if all you want is to save your data as an array:

DateTime_array = my_df["DataTime"].tolist()
S1_array = my_df["S1"].tolist()

As I said, when dealing with data - pandas' DataFrame object is the right way to manipulate data, so no need to extract values to arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

The parse_dates flag of pandas read_csv does not parse datetime properly. A custom parser should be seperately provided to it.
My minor experience with parse_dates was absolutely fine. Thank you for this comment, I'm glad it's here if someone get stuck because of that flag
1

You can use Python's Pandas to solve your problem. This is what you need:

import pandas as pd

df = pd.read_csv(csv_file_path, sep=";")
df["DateTime"] = pd.to_datetime(df["DateTime"]) 
df.set_index("DateTime").plot()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.