1

I'm trying to import a csv-file with the following content in each line.

0;0;0;1;239.57558842082713;0.3690280072634046;[239.6369763080322, 239.5252233951102, 240.21580279356058, 239.86250730788123]

df = pd.read_csv('dataset', sep=';');

In the end df.dtypes shows that it is an object, but I want to get the values that I can make for example a line plot.

I tried to convert the object to a string, to remove the '[', ']' and than cast it to numeric, but I was not successful.

Any hints?

Thanks

jpp
  • 159,742
  • 34
  • 281
  • 339
andife
  • 57
  • 6
  • The column is an `object` type, because a list is an object in python. How do you want to handle the values in the list? Do you want them to be [separate columns](https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns), or to remain inside a list in one column? – G. Anderson Nov 20 '18 at 17:43

1 Answers1

0

You can read the "list" series as a string, use ast.literal_eval, then construct a dataframe and join onto your original dataframe. The resultant series will all have numeric dtypes.

Here's an example:

from io import StringIO
from ast import literal_eval

x = StringIO("""0;0;0;1;239.57558842082713;0.3690280072634046;[239.6369763080322, 239.5252233951102, 240.21580279356058, 239.86250730788123]
0;0;0;1;239.57558842082713;0.3690280072634046;[239.6369763080322, 239.5252233951102, 240.21580279356058, 239.86250730788123]""")

df = pd.read_csv(x, header=None, sep=';')

list_cols = pd.DataFrame(df.pop(6).apply(literal_eval).values.tolist()).add_suffix('_L')

df = df.join(list_cols)

print(df)

   0  1  2  3           4         5         0_L         1_L         2_L  \
0  0  0  0  1  239.575588  0.369028  239.636976  239.525223  240.215803   
1  0  0  0  1  239.575588  0.369028  239.636976  239.525223  240.215803   

          3_L  
0  239.862507  
1  239.862507  
jpp
  • 159,742
  • 34
  • 281
  • 339
  • Hi, this goes into the desired direction, but my way is still too complicate. I want to make line graphs containing the 4 values as y-coordinates, ` list_cols = pd.DataFrame(df.pop(6).apply(literal_eval).values.tolist()).add_suffix('_pixL') df = df.join(list_cols) plt.plot([df['0_pixL'],df['1_pixL'],df['2_pixL'],df['3_pixL']]) ` – andife Nov 20 '18 at 21:09
  • It seems I've solved your problem: `I tried to convert the object to a string, to remove the '[', ']' and than cast it to numeric, but I was not successful.` If you have a new question, feel free to [ask a new question](https://stackoverflow.com/questions/ask). – jpp Nov 20 '18 at 21:16