DrakeMurdoch
DrakeMurdoch

Reputation: 859

Pandas is importing a column with square brackets as a string instead of list

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

Answers (1)

Andrej Kesely
Andrej Kesely

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

Related Questions