gpt4 book ai didi

python Pandas : Combine Dataframes that are unevenly filled

转载 作者:行者123 更新时间:2023-12-03 19:05:58 26 4
gpt4 key购买 nike

再会,
从我们的一位客户那里,我们得到了看起来像这样的 csv 导出:

id  |  name  |  object_a  |  amount_a  |  object_b  |  amount_b  |  object_c  |  amount_c
1 abc object_1 12 none none none none


id | name | object_a | amount_a | object_b | amount_b | object_c | amount_c
2 def object_2 7 object_3 19 none none


id | name | object_a | amount_a | object_b | amount_b | object_c | amount_c
3 ghi object_4 25 none none none none
现在我真的只关心这对对象(对象名称和数量)。在每组数据中,最大对数总是相同的,但它们是随机填充的。
我的问题:是否可以将它们全部加载到数据帧中并将它们转换为这样的:
object   |   amount
object_1 12
object_2 7
object_3 19
object_4 25
将所有这些 csv-exports 加载到单个数据帧中不是问题,但 panda 是否包含此类问题的解决方案?
感谢你的帮助!

最佳答案

第一 concat所有的 csvs,然后使用 pd.wide_to_long :

csv_paths = ["your_csv_paths..."]

df = pd.concat([pd.read_csv(i) for i in csv_paths]).replace("none", np.NaN)

print (pd.wide_to_long(df, stubnames=["object","amount"],
i=["id","name"],j="Hi", suffix="\w*",
sep="_").dropna())

object amount
id name Hi
1 abc a object_1 12
2 def a object_2 7
b object_3 19
3 ghi a object_4 25

关于 python Pandas : Combine Dataframes that are unevenly filled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63645473/

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