Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a csv file with a list of elements into pandas dataframe

I have a CSV file in the following format:

index      A      B      C
ind1    [1,2,3][3,4,5][6,7,8]
ind2    [1,4,3,4,8][9,1,2,1,4][3,7,3,5,9]
ind3    [2,8][1,8][1,5]

where each cell (say A,ind1)has a list [1,2,3]. When I import this into a dataframe:

df=pd.read_csv('filename.csv')

I get a dataframe in the same format as the csv, however, the list in individual cell is imported as a string.

Say I index the first element of ind1 column A df.iloc[0]['A'] gives me '[' instead of 1

basically it is reading [1,2,3] as one long string instead of reading it as a list.

How do I convert the values in all the cells into list?

like image 783
kdba Avatar asked Jul 05 '18 22:07

kdba


People also ask

Can you put a list in a pandas DataFrame?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.


1 Answers

Interesting question. If you will use pandas i would use literal_eval to encode at first the list.

import pandas as pd from ast import literal_eval df = pd.read_csv("filename.csv")

Than you can use pandas applymap to apply the literal_eval function to every cell.

df[['A', 'B', 'C']] = df[['A', 'B', 'C']].applymap(literal_eval)

Now, you have extracted the list and you can react with them like a normal list. e.g. this will give you from the first list the first element:

df.iloc[0]['A'][0]

I hope that will help you

like image 183
MarkusOdenthal Avatar answered Oct 10 '22 07:10

MarkusOdenthal