简体   繁体   English

从 Pandas 中的字符串切片创建列

[英]Creating a column from a string slice in Pandas

does somebody know why is this displaying a NaN value in the column "2_stars"??有人知道为什么这会在“2_stars”列中显示 NaN 值? 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. Pandas str.slice使用标量数字,而不是所有列值。 So need processing per rows in DataFrame.apply :所以需要在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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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