我有以下DataFrame:
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
我对如何解决这个问题的一些想法感到高兴。
答案 0 :(得分:2)
假设pandas
,只要索引中没有重复,您就可以使用set_index
和unstack
来执行您要执行的操作:
>>> 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
答案 1 :(得分:1)
假设您已经知道输出表的行名和列名,并且您的输入是选项卡分隔值的文本文件,我会做这样的事情,
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)