我是 python 的新手,并試圖改進這個測驗 df。我能夠應用正則運算式函式來查找單元格中的模式,然后向我提供單個列級別上有多少個 1 的計數。下面是將函式單獨應用于列的原始和結果 df 的影像,以及各個列的代碼。下面也是測驗 df 的文本版本(從影像中缺少一行以使其更容易共享/影像與測驗 df 不匹配但結果相似):
import pandas as pd
df = pd.DataFrame([["1 | | Had a Greeter welcome clients 1 | | Take measures to ensure a safe and organized distribution 1 | | Protected confidentiality of clients (on social media, pictures, in conversation, own congregation members receiving assistance, etc.)",
"1 | | Chairs for clients to sit in while waiting 1 | | Take measures to ensure a safe and organized distribution"],
["1 | Financial literacy/budgeting 1 | | Monetary/Bill Support 1 | | Mental Health Services/Counseling",
"1| | Clothing Assistance 1 | | Healthcare 1 | | Mental Health Services/Counseling 1 | | Spiritual Support 1 | | Job Skills Training"]
] , columns = ['CF1', 'CF2'])


pattern = re.compile(r'\d ')
df['CF1test'] = df['CF1'].apply(lambda x: '_'.join(pattern.findall(x))).str.count(pattern)
df['CF2test'] = df['CF2'].apply(lambda x: '_'.join(pattern.findall(x))).str.count(pattern)
這是我試圖開發以迭代并將相同的函式應用于多個列的回圈示例:
for column in df:
df[column] = df.join([column](df.apply(lambda x: '_'.join(pattern.findall(x))).str.count(pattern), rsuffix = '_test'))
但是,我收到“型別錯誤:預期的字串或類似位元組的物件”并且我迷路了。有沒有更好的方法可用?任何建議都會很棒!
uj5u.com熱心網友回復:
您可以stack使用您的資料框并extractall與您的模式一起使用:
(df.stack().str.extractall('(\d )')[0]
#.astype(int) # conversion to int is only required for other operations, like sum
.groupby(level=[0,1]).count().unstack())
輸出:
CF1 CF2
0 3 2
1 3 5
注意。我在這里計算數字(任何數字,因此“test123”中的“42”或“123”也將被計算在內),如果你想限制為1s,你可以調整正則運算式,如果你想你也可以執行其他操作,如求和而不是計數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/369401.html
下一篇:計算欄位中單詞/字符的出現次數
