如何計算字串 col 中的值計數?
df col
0 fruit["apple"], colour["green", "yellow" ]
1 colour["yellow"]
2 colour["brown"]
預期產出
fruit 1
colour 3
uj5u.com熱心網友回復:
Series.str.extractall與由正則運算式連接的子字串一起使用,|或者:
s = df['col'].str.extractall('(fruit|colour)')[0].value_counts()
print (s)
colour 3
fruit 1
Name: 0, dtype: int64
[或獲取更多動態解決方案之前的單詞:
s = df['col'].str.extractall(r'(\w )\[')[0].value_counts()
print (s)
colour 3
fruit 1
dtype: int64
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/519744.html
標籤:Python熊猫列表
