1

I have a .csv file containing several columns of which one contains multiple values:

"column1";"column2";"column3";
some_string; 1 ; 1,2,3,4 ;

Now, I would like to import the file using pandas read_csv:

import pandas as pd
df = pd.read_csv('file.csv', sep=';')

This obviously leads to the issue that the multiple values get imported as a single string object. Is there any way I could import these values as a list or an array that I could even call a single value in that cell? That would be tremendously helpful!

Thank you in advance!

1
  • You can convert the string to a list as post-load step. using df.column3.str.split() Commented Feb 26, 2020 at 9:47

2 Answers 2

1

You can do this after loading the data, you would need a lambda function:

df['column3'] = df['column3'].apply(lambda x: str(x).split(","))

Check type of cell, this gives list:

type(df.iloc[0,2])

Inspect the length, this is 4:

len(df.iloc[0,2])

Select an element in your list:

df.iloc[0,2][0]

Turn the list elements in integers:

df['column3'] = df['column3'].apply(lambda x: list(map(int, x)))

Inspect type:

type(df.iloc[0,2][0])
Sign up to request clarification or add additional context in comments.

2 Comments

That's great and it works! The only thing I've observed using type(df.iloc[0,2][0]) that the elements within the list are indeed stored as a string not an integer. Can I do something about that as well? I mean for calculations, I could simply call it with int(dfSimp.iloc[0,2][0]) but the value itself would remain as a string.
@nic: it is my pleasure! I have added a line of code that turns the elements into integers.
0

You can try converters argument of pd.read_csv to pass a function for the specified col:

>>> pd.read_csv(s, sep=';', usecols = range(3), 
                converters = {'column3':lambda x:x.split(',')})

       column1  column2         column3
0  some_string        1  [ 1, 2, 3, 4 ]
# or,
>>> pd.read_csv(s, sep=';', 
                converters = {'column3':lambda x:x.split(',')}).dropna(1)
       column1  column2         column3
0  some_string        1  [ 1, 2, 3, 4 ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.