Home > Enterprise >  How to read a csv with a malformed column of lists and plot the values
How to read a csv with a malformed column of lists and plot the values

Time:10-11

I have a file csv with the following columns

Customer List_Items
J.B.      [13,2,3,4,42,12]
F.C.      [1,44,51,24,12,53]
D.S.      NA 
B.V.      *
F.R.      [1,0,0,0,0,0]

I have max six items. The elements in the lists above are the frequency. The elements above are strings, not lists, since the file csv was created manually outside python coding. I would like to plot a multiline charts where, on the y-axis I have the frequency and on the x-axis the labels 1,2,3,4,5,6. To exclude values not in scope I could filter out NA and *. But I do not know how to plot lists in a multiline plot. Should I expand the lists in separate columns?

For example: J.B. would be represented by a line with

x-axis y-axis
1      13
2      2
3      3
4      4
5      42
6      12

Similarly for the other customers.

CodePudding user response:

  • A column can be converted from strings back to lists using:
    • df = pd.read_csv('data.csv', sep='\\s ', index_col=[0], converters={'List_Items': literal_eval}) - This will not work for this OP
      • See this enter image description here

  • Related