重新整理数据框架 – 将具有重复值的列值转换为列标题

huangapple go评论90阅读模式
英文:

Reshape DataFrame - Convert column value with duplicates to column header

问题

I want to change the shape of the dataframe.

我想要改变数据框的形状,将"ITEM"、"MANUFACTURER"、"MODEL"和"COST"作为列名,并将相应的值放在行中。

英文:

I have a dataframe with 2 columns. The first columns has string df[0]=[a,b,c]*5.My second column has corresponding values[cat,dog,4...etc].I want to change the shape of dataframe.

               0                   1
0           ITEM         cat
1   MANUFACTURER         queen
2          MODEL               black
3           COST              5
4           ITEM          dog
5   MANUFACTURER                god
6          MODEL   All-in-Wonder White
7           COST              50

I want Item Manufacturer Model and Cost as column Names and corresponding values in rows.

答案1

得分: 1

It sounds like you want a pandas.pivot operation.

For your data, it might look like this:

import pandas as pd

data = pd.DataFrame({
    0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
    1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})

# Encode how rows should be grouped
data['index'] = [1, 1, 1, 1, 2, 2, 2, 2]

data.pivot(values=1, columns=0, index='index')
0     COST ITEM MANUFACTURER                MODEL
index                                            
1        5  cat        queen                black
2       50  dog          god  All-in-Wonder White
英文:

It sounds like you want a pandas.pivot operation.

For your data, it might look like this:

import pandas as pd

data = pd.DataFrame({
    0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
    1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})

# Encode how rows should be grouped
data['index'] = [1,1,1,1,2,2,2,2]

data.pivot(values=1, columns=0, index='index')
0     COST ITEM MANUFACTURER                MODEL
index                                            
1        5  cat        queen                black
2       50  dog          god  All-in-Wonder White

答案2

得分: 0

import pandas as pd

data = pd.DataFrame({
    0: 2 * ["项目", "制造商", "型号", "成本"],
    1: ["猫", "女王", "黑色", 5, "狗", "上帝", "全能白", 50]
})
transformed_data = pd.DataFrame(
    dict(data
         .groupby(0)
         .agg(lambda x: tuple(x))
         .reset_index()
         .to_dict(orient='split')['data'])
)
英文:
import pandas as pd

data = pd.DataFrame({
    0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
    1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})
transformed_data = pd.DataFrame(
    dict(data
         .groupby(0)
         .agg(lambda x: tuple(x))
         .reset_index()
         .to_dict(orient='split')['data'])
)

huangapple
  • 本文由 发表于 2020年1月3日 22:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定