简体   繁体   中英

Splitting Pandas DF converts it into list

Here's my dataframe

熊猫数据框

In Cover Design column there are few values that end with "- undefined" and I want to remove that. So, I used split function to remove

test[['ttt']] = test['Cover Design'].str.split(' - undefined')

and this is what I got

在此处输入图片说明

But the values in new column are in list type how to get it to string?

Your solution should be changed for str[0] for select first values of lists after split :

test['ttt'] = test['Cover Design'].str.split(' - undefined').str[0]

Another solution is use Series.str.replace :

test['ttt'] = test['Cover Design'].str.replace(' - undefined', '')

If need specify end of string by regex $ :

test['ttt'] = test['Cover Design'].str.replace(' - undefined$', '')

Bad solution is use strip , because it remove all values from - undefined from end and start of string, dont use it:

test['ttt'] = test['Cover Design'].str.strip(' - undefined')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM