gpt4 book ai didi

python Pandas : Merge Columns of Data Frame with column name into one column

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:49 27 4
gpt4 key购买 nike

我的数据框中有以下格式的数据:

>>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
>>> df
A B C D
0 0.578095 -1.985742 -0.269517 -0.180319
1 -0.618431 -0.937284 0.556290 -1.416877
2 1.695109 0.122219 0.182450 0.411448
3 0.228466 0.268943 -1.249488 3.227840
4 0.005990 -0.805618 -1.941092 -0.146649
5 -1.116451 -0.649854 1.272314 1.422760

我想通过附加行数据和列名创建以下输出来组合每一行的一些列:

          A         B        New Column
0 0.578095 -1.985742 {"C":"-0.269517","D":"-0.180319"}
1 -0.618431 -0.937284 {"C":"0.556290","D":"-1.416877"}
2 1.695109 0.122219 {"C":"0.182450","D":"0.411448"}
3 0.228466 0.268943 {"C":"-1.249488","D":"3.227840"}
4 0.005990 -0.805618 {"C":"-1.941092","D":"-0.146649"}
5 -1.116451 -0.649854 {"C":"1.272314","D":"1.422760"}

我怎样才能在 pandas 中实现这一点?

最终游戏是将数据采用 JSON 格式,其中 C-D 列作为维度 A-B 的度量,然后将它们存储到 Snowflake 中的表中。

最佳答案

删除列并使用 agg 创建一个新列:

df2 = df.drop(['C', 'D'], axis=1).assign(New_Column=
df[['C', 'D']].agg(pd.Series.to_dict, axis=1))

df2
A B New_Column
0 -0.645719 -0.757112 {'D': 0.8923148471642509, 'C': -0.685995130541...
1 -0.124200 -0.578526 {'D': -0.5457121278891495, 'C': -1.46006615752...
2 2.160417 -0.985475 {'D': -0.49915307027471345, 'C': 0.85388172610...
3 2.111050 1.384887 {'D': -0.4617380879640236, 'C': 0.907519279458...
4 0.781630 -0.366445 {'D': -0.3105127375402184, 'C': 0.295808587414...
5 0.460773 0.549545 {'D': -0.993162129461116, 'C': 0.8163378188816...

关于 python Pandas : Merge Columns of Data Frame with column name into one column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616996/

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