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?
You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With