Reputation: 859
Using pd.read_csv
I am importing a dataframe. One of the columns contains lists of strings. For example:
>>> df['topic'].head(5)
0 ['ECONOMIC PERFORMANCE', 'ECONOMICS', 'EQUITY ...
1 ['CAPACITY/FACILITIES', 'CORPORATE/INDUSTRIAL']
2 ['PERFORMANCE', 'ACCOUNTS/EARNINGS', 'CORPORAT...
3 ['PERFORMANCE', 'ACCOUNTS/EARNINGS', 'CORPORAT...
4 ['STRATEGY/PLANS', 'NEW PRODUCTS/SERVICES', 'C...
Name: topic, dtype: object
Though this column should be full of lists, pandas is importing it as strings. How can I get pandas to import this as a column of lists?
Upvotes: 3
Views: 805
Reputation: 195468
You can convert the column with strings to Python lists with ast.literal_eval
. For example:
from ast import literal_eval
df["topic"] = df["topic"].apply(literal_eval)
print(df)
Prints:
topic
0 [ECONOMIC PERFORMANCE, ECONOMICS, EQUITY]
1 [CAPACITY/FACILITIES, CORPORATE/INDUSTRIAL]
2 [PERFORMANCE, ACCOUNTS/EARNINGS, CORPORATE]
3 [PERFORMANCE, ACCOUNTS/EARNINGS, CORPORATE]
4 [STRATEGY/PLANS, NEW PRODUCTS/SERVICES]
Upvotes: 3