Convert pandas dataframe column with string array (Python)

Asked

Viewed 284 times

-1

I have a data frame pandas in Python, in which one of the columns is bringing the values in an array(list). I would like to remove the brackets ([]), to convert into string. I have tried with strip, regex and nothing.

It’s like this:

Está assim

I’d like to keep it that way:

Gostaria que ficasse assim:

2 answers

0


You can use the pandas applymap together with isinstance:

df.applymap(lambda x: x if not isinstance(x, list) else " ".join(x))

The data frame receives the value of x without modifications if it is not a list, if a list makes a concatenation of the elements. Works for empty list.

0

A way that worked properly and gets very fast was:

df['liststring'] = df['lists'].apply(lambda x: x[1:-1])

The new column liststring receives the string of the existing list in df['lists'].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.