当我需要将行信息合并为列时,如何合并两个数据框?

问题描述 投票:0回答:2

我有两个数据框,第一个包含产品的统计信息,第二个是我创建的空数据框。我需要从第一个数据框中获取数据并将其插入到第二个数据框中。我只知道如何连接列和行中的数据,有办法做到这一点吗?

product = 'PRDQM0011122TR131'
prod_results = pm.summary(trace_dict_prodlevel[product])
prod_results

          | mean     | sd                 |
          | -------- | --------           |
Slope     | -0.012   | -0.012            |
 Intercept| 0.906    | 0.356              |

prod_results1 = pd.DataFrame(columns = ['product', 'slope_mean', 'slope_sd', 'int_mean', 'int_sd'])
prod_results1

| product | slope_mean | slope_sd  |int_mean |int_sd|
|---------|------------|-----------|---------|------|

期望的输出是这样的:

|产品 |斜率_平均值 |斜率_sd |int_mean |int_sd|

|PRDQM0011122TR131|-0.012 |-0.012 |0.906 |0.356 |

我认为 pd.concat 或 pd.join 会起作用,但我不知道如何将它们应用到这种情况。

python pandas dataframe join concatenation
2个回答
0
投票

您可以直接将数据从

prod_results
数据框输入到
prod_results1
数据框。这是有效的,并且考虑到情况很简单,使用像
melt
这样的东西可能太多了。

由于您不提供代码来重现数据,这是我对工作代码的猜测:

prod_results1['product'] = product
prod_results1['slope_mean'] = prod_results.loc[prod_results.index=='Slope', 'mean']
prod_results1['slope_sd'] = prod_results.loc[prod_results.index=='Slope', 'sd']
prod_results1['int_mean'] = prod_results.loc[prod_results.index=='Intercept', 'mean']
prod_results1['int_sd'] = prod_results.loc[prod_results.index=='Intercept', 'sd']



0
投票

这与您所问的类似。您也可以将此代码作为循环运行。循环应该只更新

product
df

df1 = df.melt(value_vars = ['slope', 'intercept'], var_name = 'stat', value_name = 'val' )
mapping = {
    0: "slope_mean",
    1: "slope_sd",
    2: "int_mean",
    3: "int_sd"
}

# Apply the mapping to the 'stat' column
df1["stat"] = df1.index.map(mapping)
df1 = df1.set_index('stat').T
df1['product'] = 'prod1'
prod_results1 = pd.concat([prod_results1, df1], axis = 0)

print(prod_results1)

product  slope_mean  slope_sd  int_mean  int_sd
val   prod1      -0.012     0.906    -0.012   0.356
© www.soinside.com 2019 - 2024. All rights reserved.