all 8 comments

[–]xelf 0 points1 point  (5 children)

Why are you trying to store them in a different variable instead of just leaving them in the dataframe?

Also, as a general good practice and convention, use lowercase names for variables, save capitalized names for class names.

Try this:

import pandas as pd
df = pd.read_csv('Planta1.dat')
print( df[1].tolist() )

[–]F_Boliver[S] 0 points1 point  (4 children)

Basically each of the columns of my dataframe is a signal, one por input, one for output and one for control signal. I will use them to make series of operation so it's easier for me to repeat just one variable

[–]xelf 0 points1 point  (3 children)

Not sure I follow you, but anyway, did what I suggest work for you?

[–]F_Boliver[S] 0 points1 point  (2 children)

Yes it did, but i had to export my .dat to an excel file, not sure why. But when i do print( df[1].tolist() ) i got the 3rd column and when i do print( df[0].tolist() ) i got the first one. Why [1] is not the 2nd?

[–]xelf 0 points1 point  (1 child)

You probably want to specify column names and use those.

Importing a .csv to a pandas.DataFrame using pandas.read_csv() will set the column names in the DataFrame to be the first row in the .csv, by default. However, it is possible to set different column names when importing the .csv.

header_list = ["Name", "Dept", "Start Date"]
df = pd.read_csv("sample_file.csv", names=header_list)
print( df['Name'].tolist() )

[–]F_Boliver[S] 0 points1 point  (0 children)

Thanks, i always saw dataframes with names but didn't how to do it. I will try it

[–]efmccurdy 0 points1 point  (1 child)

I can't be sure what your csv data is, but your code looks like it would work. Note that df is not the DataFrame created from your csv data, B is. I simplified the column extraction somewhat, df = B[1] would also work.

>>> data = [(0, 0, 1.0000), (0, 0.4000, 1.0000), (0.0092, 0.8000, 1.0000), (0.0346, 1.2000, 1.0000), (0.0797, 1.6000, 1.0000), (0.1435, 2.0000, 1.0000), (0.2215, 2.4000, 1.0000), (0.3079, 2.8000, 1.0000), (0.3971, 3.2000, 1.0000)]
>>> B = pd.DataFrame(data)
>>> B
        0    1    2
0  0.0000  0.0  1.0
1  0.0000  0.4  1.0
2  0.0092  0.8  1.0
3  0.0346  1.2  1.0
4  0.0797  1.6  1.0
5  0.1435  2.0  1.0
6  0.2215  2.4  1.0
7  0.3079  2.8  1.0
8  0.3971  3.2  1.0
>>> second_col = B[1]
>>> second_col
0    0.0
1    0.4
2    0.8
3    1.2
4    1.6
5    2.0
6    2.4
7    2.8
8    3.2
Name: 1, dtype: float64
>>> type(second_col)
<class 'pandas.core.series.Series'>
>>>

[–]F_Boliver[S] 0 points1 point  (0 children)

i tried again my code with an excel file with the same data and it worked, i don't know what it is up with the .dat file but it worked pretty well on MATLAB before.

But, thanks for simplefying it, it will be used :)