【发布时间】:2018-12-26 17:59:21
【问题描述】:
假设我有一个如下所示的 DataFrame:
df=pd.DataFrame({'name': ['john','jack','jill','al','zoe','jenn','ringo','paul','george','lisa'], 'how do you feel?': ['excited', 'not excited', 'excited and nervous', 'worried', 'really worried', 'excited', 'not that worried', 'not that excited', 'nervous', 'nervous']})
how do you feel? name
0 excited john
1 not excited jack
2 excited and nervous jill
3 worried al
4 really worried zoe
5 excited jenn
6 not that worried ringo
7 not that excited paul
8 nervous george
9 nervous lisa
我对计数感兴趣,但分为三类:“兴奋”、“担心”和“紧张”。
关键是“兴奋和紧张”应该与“兴奋”组合在一起。事实上,包含“excited”的字符串应该包含在一个组except中,用于诸如“not thatexcited”和“notexcited”之类的字符串。同样的逻辑也适用于“担心”和“紧张”。 (请注意,“兴奋和紧张”实际上属于“兴奋”和“紧张”组)
您可以看到,典型的 groupby 将不起作用,字符串搜索必须灵活。
我有一个解决方案,但想知道你们是否都可以找到更好的 Python 方法,和/或使用我可能不知道的更合适的方法。
这是我的解决方案:
定义一个函数以返回包含所需子字符串且不包含否定情绪的子字符串的行的计数
def get_perc(df, column_label, str_include, str_exclude):
data=df[col_lab][(~df[col_lab].str.contains(str_exclude, case=False)) & \
(df[col_lab].str.contains(str_include, case=False))]
num=data.count()
return num
然后,在循环内调用此函数,传入各种“str.contains”参数,并将结果收集到另一个 DataFrame 中。
groups=['excited', 'worried', 'nervous']
column_label='How do you feel?'
data=pd.DataFrame([], columns=['group','num'])
for str_include in groups:
num=get_perc(df, column_label, str_include, 'not|neither')
tmp=pd.DataFrame([{'group': str_include,'num': num}])
data=pd.concat([data, tmp])
data
group num
0 excited 3
1 worried 2
2 nervous 3
您能想到一种更简洁的方法吗?我确实在“str.contains”中尝试了一个正则表达式,以避免需要两个布尔系列和“&”。但是,如果没有捕获组,我就无法做到这一点,这意味着我必须使用“str.extract”,这似乎不允许我以相同的方式选择数据。
非常感谢任何帮助。
【问题讨论】:
-
"excited and nervous"算作两者或只是excited? -
两者都很好
-
在这种情况下,您也可以编辑您的问题。
-
我会推荐 NLTK
标签: python python-3.x pandas pandas-groupby