数据帧的特定转置

2024-09-29 23:33:23 发布

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

我有一个数据帧,我想以某种方式进行转换,“attr”列的值变成列而不是值,而price保持为列

我曾尝试对列进行分组和转置,但没有找到一种方法来达到我想要的目的。这是我的数据集:

           attr                      values   price
0         Mærke            Knauf Insulation   24.95
1   Produkttype           Bygningsisolering   24.95
2         Serie                       SPACE   24.95
3         Model                  FORMSTYKKE   24.95
4         Mærke                   Bromiflex   20.00
5   Produkttype                     Rørskål   20.00
6     Materiale       Opskummet polyethylen   20.00
7     Størrelse                      Ø18 MM   20.00
8         Mærke                   Skamowall  190.00
9   Produkttype             Isoleringsplade  190.00
10        Serie                       BASIC  190.00
11    Materiale  Brændt kalk og mikrosilika  190.00
12        Mærke                    Rockwool  210.00
13  Produkttype           Bygningsisolering  210.00
14        Serie                 Terrænbatts  210.00
15    Materiale                     Stenuld  210.00
16        Mærke            Knauf Insulation   65.00
17  Produkttype                   Isolering   65.00

我想要的是:

Mærke             Produkttype       Serie   Model      Materiale             Størrelse Price
Knauf Insulation  Bygningsisolering SPACE   FORMSTYKKE NAN                   NAN       24.95
Bromiflex         Rørskål           NAN     NAN        Opskummet polyethylen Ø18 MM    24.95     

我从df.groupby([“attr”,“values”])[“price”].mean().reset_index().set_index(“attr”)开始,但没有得到想要的结构,这很可能涉及到数据集的转换

非常感谢您的帮助


Tags: 数据modelspacenanpriceattrvaluesserie
2条回答
# produce data
df = pd.DataFrame(data=[
    ("Mærke", "Knauf Insulation", 24.95),
    ("Produkttype", "Bygningsisolering", 24.95), 
    ("Serie", "SPACE", 24.95), 
    ("Mærke", "Bromiflex", 20.00), 
    ("Produkttype", "Rørskål", 20.00), 
    ("Materiale", "Opskummet polyethylen", 20.00), 
    ("Størrelse", "Ø18 MM", 20.00), 

    
    
], 
    columns = ("attr", "values", "price")
)

# display data
df.head()

# output

attr    values                           price
0       Mærke   Knauf Insulation         24.95
1       Produkttype Bygningsisolering    24.95
2       Serie   SPACE                    24.95
3       Mærke   Bromiflex                20.00
4       Produkttype Rørskål              20.00


# transform data using *pivot* method
df = df.pivot(columns="attr", values="values", index="price").reset_index()
df.columns.name = None

# show results
df.head()

# output

price   Materiale   Mærke   Produkttype Serie   Størrelse
0   20.00   Opskummet polyethylen   Bromiflex   Rørskål NaN Ø18 MM
1   24.95   NaN Knauf Insulation    Bygningsisolering   SPACE   NaN

import io
import pandas as pd

df = pd.read_csv(io.StringIO("""           attr                      values   price
0         Mærke            Knauf Insulation   24.95
1   Produkttype           Bygningsisolering   24.95
2         Serie                       SPACE   24.95
3         Model                  FORMSTYKKE   24.95
4         Mærke                   Bromiflex   20.00
5   Produkttype                     Rørskål   20.00
6     Materiale       Opskummet polyethylen   20.00
7     Størrelse                      Ø18 MM   20.00
8         Mærke                   Skamowall  190.00
9   Produkttype             Isoleringsplade  190.00
10        Serie                       BASIC  190.00
11    Materiale  Brændt kalk og mikrosilika  190.00
12        Mærke                    Rockwool  210.00
13  Produkttype           Bygningsisolering  210.00
14        Serie                 Terrænbatts  210.00
15    Materiale                     Stenuld  210.00
16        Mærke            Knauf Insulation   65.00
17  Produkttype                   Isolering   65.00"""), sep="\s\s+", engine="python")


df.assign(prod_idx=df["attr"].eq("Mærke").cumsum()).pivot(
    index=["prod_idx", "price"], columns="attr", values=["values"]
).droplevel(0,1).reset_index()
^{tb1}$

相关问题 更多 >

    热门问题