我正在使用多个这样的列表:
restaurants = ['MCDONALD', 'DONUT', 'FOOD COURT', 'WHATABURGER']
这些列表只是从我的银行csv文件的“描述”数据框中拉出那些键盘。这些项目中的每一项在我要隔离的同一行中都有对应的借方金额。我还想在过程中重命名数据框以显示如下内容:
Restaurants: $43.00
当前运行时:
restaurant_total = df.loc[df['Description'].str.contains('|'.join(restaurants), flags = re.I, regex = True)].reset_index(drop = True).sum()
它打印出:
Description MCDONALD'S F24712 LANE...
Debit 43.00
Credit 0
dtype: object
[Finished in 0.683s]
我想摆脱默认说明(mcdonalds部分),并用更合适的内容代替,然后仅显示借方金额。
答案 0 :(得分:1)
IIUC,您可以创建一个帮助者列并进行分组
import pandas as pd
df = pd.DataFrame({'Description' : ['McDonalds','WhatABurger','Donuts',"Pepe's"],
'debit' : [50, 33,250,3.99]})
restaurants = ['MCDONALD', 'DONUT', 'FOOD COURT', 'WHATABURGER']
df.loc[
df["Description"].str.contains("|".join(restaurants), case=False, regex=True),
"Type",
] = "Restuarant"
new_df = df.groupby('Type')['debit'].sum().to_frame()
print(new_df)
debit
Type
Restuarant 333.0