csv中的pandas表转换

时间:2018-07-17 08:05:17

标签: python list pandas transformation

您能帮我解决熊猫遇到的问题吗,我想为excel导出一个列表,这样看起来不错但不能

它应该看起来像这样:

 cut    AY1 AY2 A_ges
    0   values  values  values
    5   values  values  values
    10  values  values  values
    15  values  values  values
    20  values  values  values
    25  values  values  values

我收集的数据如下:

[name, cut, values]
['AY1', '+0.000', '25.71']
['AY1', '+5.000', '25.71']
['AY1', '+10.000', '26.0']
['AY1', '+15.000', '25.9']
['AY1', '+20.000', '25.85']
['AY1', '+25.000', '25.95']
['AY2', '+0.000', '26.01']
['AY2', '+5.000', '26.62']
['AY2', '+10.000', '26.55']
['AY2', '+15.000', '26.5']
['AY2', '+20.000', '26.56']
['AY2', '+25.000', '26.62']
['A_ges', '+0.000', '632.2']
['A_ges', '+5.000', '633.28']
['A_ges', '+10.000', '633.85']
['A_ges', '+15.000', '633.76']
['A_ges', '+20.000', '633.72']
['A_ges', '+25.000', '634.23']

1 个答案:

答案 0 :(得分:0)

我确实将您的输入数据更改为列表,可能有更好的方法来解决此问题,但这是一个快速而肮脏的解决方案:

import pandas as pd

data = [
        ['AY1', '+0.000', '25.71'],
        ['AY1', '+5.000', '25.71'],
        ['AY1', '+10.000', '26.0'],
        ['AY1', '+15.000', '25.9'],
        ['AY1', '+20.000', '25.85'],
        ['AY1', '+25.000', '25.95'],
        ['AY2', '+0.000', '26.01'],
        ['AY2', '+5.000', '26.62'],
        ['AY2', '+10.000', '26.55'],
        ['AY2', '+15.000', '26.5'],
        ['AY2', '+20.000', '26.56'],
        ['AY2', '+25.000', '26.62'],
        ['A_ges', '+0.000', '632.2'],
        ['A_ges', '+5.000', '633.28'],
        ['A_ges', '+10.000', '633.85'],
        ['A_ges', '+15.000', '633.76'],
        ['A_ges', '+20.000', '633.72'],
        ['A_ges', '+25.000', '634.23']
        ]

df = pd.DataFrame(data)

indices = df[1].unique()
cols = df[0].unique()
df2 = pd.DataFrame(index=indices, columns=cols)

grouped = df.groupby(by=1, axis=0)
for key,value in grouped:
    df2.loc[key,:] = value.loc[:,2].values

print(df2)