简体   繁体   中英

Creating a column from a string slice in Pandas

does somebody know why is this displaying a NaN value in the column "2_stars"?? Thanks in advance

data['1_star']=data['Sentiment'].str.slice(31,40)
data['start'] = data['Sentiment'].str.find("'2 stars', 'score': ") + len("'2 stars', 'score': ")
data['end'] = data['Sentiment'].str.find("}, {'label': '3 stars'")
data['2_stars']=data['Sentiment'].str.slice(data['start'],data['end'])

在此处输入图片说明

Pandas str.slice working with scalars numbers, not by all columns values. So need processing per rows in DataFrame.apply :

data['2_stars']= data.apply(lambda x: x['Sentiment'][slice(x['start'], x['end'])], axis=1)

Another idea with list comprehension:

zipped = zip(data['Sentiment'], data['start'], data['end'])
data['2_stars'] = [a[slice(s, e)] for a, s, e in zipped]

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