gpt4 book ai didi

python - 在Python中的数据框中搜索逗号分隔字符串中的数字

转载 作者:行者123 更新时间:2023-12-01 00:54:28 27 4
gpt4 key购买 nike

df1:

id, Name, Lastname, Account, ProofID1, ProofID2, transaction_codes_history
1, ab1, dc1, 312, 1224, 111, 0, 1.3, 2.1, 3, 4.1, 9.1, 1.1, 7.2
2, ab2, dc2, 434, 1225, 112, 0, 1.2, 2.1, 1.4, 3, 4.4, 12.2
3, ab3, dc3, 578, 1226, 111, 0, 1.2, 2.1, 1.4, 3, 4.2, 12.1
4, ab4, dc4, 624, 1227, 112, 0, 1.1, 7.2
5, ab5, dc5, 684, 1228, 113, 0, 1.3, 2.1, 3, 9.2, 11.2

我正在尝试根据特定列的transaction_codes_history复制特定列。

如果 transaction_codes_history 至少包含 4.17.21.1,那么我们会将这些列复制到数据框。

transaction_codes_history 是逗号分隔的 float 字符串。

预期输出:

Name, Account, ProofID1, ProofID2
ab1, 312, 1224, 111
ab3, 578, 1225, 111
ab4, 624, 1227, 112

最佳答案

您可以通过创建附加列来完成此操作,例如:

df["to_keep"] = df["transaction_codes_history"].str.contains("1.1|4.1|7.2", regex=True)

那么你可以只保留你想要的行:

df[df["to_keep"]][["id", "Name", "Lastname", "Account"]]

希望对你有帮助尼古拉斯

关于python - 在Python中的数据框中搜索逗号分隔字符串中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com