[英]pandas groupby of DataFrame using Series of substrings
我有一個熊貓DataFrame,我想按其中一列的子字符串進行分組。 子字符串在另一個pandas系列(或列表)中給出。 我已經嘗試了很多方法,但是我根本無法使其正常工作。
我有這個:
tst = pd.DataFrame({'id': [0, 11, 222, 3333, 44444],
'bla': ['ab', 'ba', 'ca', 'bc', 'db']})
test = pd.Series(['a', 'b', 'c', 'd'])
我想組tst
根據'A', 'B', 'C', 'd'(從是否test
)被包含在tst['bla']
df.apply()最好在這里。
import pandas as pd
def funcx(x, test_str):
return test_str in x['bla']
tst = pd.DataFrame({'id': [0, 11, 222, 3333, 44444],
'bla': ['ab', 'ba', 'ca', 'bc', 'db']})
test = pd.Series(['a', 'b', 'c', 'd'])
result = {}
for xstring in test:
result[xstring] = tst.apply(funcx, args=( xstring), axis=1)
print result
給我們;
{'a': 0 True
1 True
2 True
3 False
4 False
dtype: bool, 'c': 0 False
1 False
2 True
3 True
4 False
dtype: bool, 'b': 0 True
1 True
2 False
3 True
4 True
dtype: bool, 'd': 0 False
1 False
2 False
3 False
4 True
dtype: bool}
然后可以用來選擇相關的行;
>>print tst[result['a']]
bla id
0 ab 0
1 ba 11
2 ca 222
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.