gpt4 book ai didi

python - pandas csv到行和列的数据框转换

转载 作者:行者123 更新时间:2023-11-30 22:07:24 27 4
gpt4 key购买 nike

我有这个 CSV 数据

C,kernel,error
0.001,linear,0.03
0.001,poly,0.04
0.01,linear,0.01
0.01,poly,0.03
0.1,linear,0.00
0.1,poly,0.02
1.0,linear,0.00
1.0,poly,0.01
10.0,linear,0.00
10.0,poly,0.01

使用 pandas,如何创建一个包含一列唯一 C 值、与该 C 值关联的内核以及错误的数据帧?

预期输出

C, linear, poly
0.001,0.03,0.04 <-- error values
0.01,0.01,0.03
0.1,0.00,0.02
1.0,0.00,0.01
10,0.00,0.01

最佳答案

您可以使用pd.DataFrame.pivot :

res = df.pivot(index='C', columns='kernel', values='error')

print(res)

kernel linear poly
C
0.001 0.03 0.04
0.010 0.01 0.03
0.100 0.00 0.02
1.000 0.00 0.01
10.000 0.00 0.01

关于python - pandas csv到行和列的数据框转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52470997/

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