重新排列选项卡中的数据

2024-05-19 22:46:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

dis Country Price 0 0.8 US 500 1 0.8 England 1000 2 0.8 Spain 1500 3 0.8 Portugal 600 4 0.8 Germany 900 5 0.9 US 2200 6 0.9 England 3000 7 0.9 Spain 600 8 0.9 Portugal 1000 9 0.9 Germany 4000

不过,我想重新安排一下:

dis US England Spain Portugal Germany
0.8 500 1000 1500 600 900 0.9 2200 3000 600 1000 4000

我会很乐意解决这个问题的。在


Tags: 数据countrypriceusdis我会spain乐意
1条回答
网友
1楼 · 发布于 2024-05-19 22:46:57

假设您已经知道输出表的行和列名称,并且您的输入是一个由制表符分隔的值组成的文本文件,那么我将做如下操作

afile = open("input.csv","r")
content = [k.split("\t") for k in afile.read().slit("\n")]
#If you already have a list of lists these first 2 lines are unnecessary.

output = {}
for k in content:
    if not(k[1] in output.keys):
        output[k[1]] = {}
    output[k[1]][k[2]] = k[3]

print(output)
网友
2楼 · 发布于 2024-05-19 22:46:57

假设pandas,只要索引中没有重复,就可以使用set_index和{}来执行您要执行的操作:

>>> import pandas as pd
>>> df = pd.DataFrame({'dis': [0.8, 0.8, 0.9, 0.9], 'Country':['US', 'England', 'US', 'England'], 'Price':[500, 1000, 1500, 2000]})
>>> df
    Country Price   dis
0   US      500     0.8
1   England 1000    0.8
2   US      1500    0.9
3   England 2000    0.9
>>> df.set_index(['dis', 'Country']).unstack()
        Price
Country England US
dis     
0.8     1000    500
0.9     2000    1500

相关问题 更多 >