使用列内的关键字对CSV文件进行排序

l2osamch  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(110)

我尝试使用python对银行对账单上的交易进行分组。交易列在CSV文件中。我无法让它使用部分描述而不是整个描述。
我尝试了下面的代码,但它不工作,因为它使用了整个描述。(即AMZN MKTP 09/01 PURCHASE)这是太具体了。我希望它只使用“AMZN”排序。

grouped = test.groupby(['Description'])
for key, item in grouped:
    print('Key is: ' + str(key))
    print(str(item), '\n\n')
r7xajy2e

r7xajy2e1#

您可以创建包含分组所依据的信息的额外列:

test['group_key'] = test['Description'].apply(lambda x: x.split()[0])
grouped = test.groupby(['group_key'])
for key, item in grouped:
    print('Key is: ' + str(key))
    print(str(item), '\n\n')

注意我假设description是空格可分的,你只需要第一个单词。

相关问题