熊猫合并数据框列

时间:2019-10-31 14:47:03

标签: python pandas dataframe merge

我输入的数据帧是:

this.finger.isAvailable().then((available) => {
  if (available === "finger") {
    this.finger.show({
      clientId: 'qwerty',
      clientSecret: 'AnyPassword123',
      disableBackup: true
    }).then((result) => {
      console.log(JSON.stringify(result));
      this.modalCtrl.dismiss();
    }).catch((e) => {
      console.log(JSON.stringify(e));
    })
  }
}).catch((err) => {
  this.modalCtrl.dismiss();
});

所需的输出为;

df1

Grp       A       B       C
Men       10      15      14
Women     4       6       5
Baby      3       5       15

df2

Grp       Upper_A    Lower_A       Upper_B    Lower_B       Upper_C   Lower_C
Men       10         1             9          2             15        2
Women     6          4             10         4             10        3
Baby      5          3             15         7             6         3

您能帮我吗? PS:评估列是根据是否介于上限值和下限值之间分配的。

2 个答案:

答案 0 :(得分:2)

创建解决方案MultiIndex,方法是将列拆分并通过DataFrame.stack重塑形状:

df2.columns = df2.columns.str.split('_', expand=True)

df2 = df2.stack().rename_axis(('Grp','Features'))

然后以相同方式处理df1,并通过以下方式创建一列DataFrameSeries.to_frameDataFrame.join秒,df2,最后使用Series.between作为新列:

df1 = df1.rename_axis('Features', axis=1).stack().to_frame('Values')

df = df1.join(df2).reset_index()
df['Evaluation'] = df['Values'].between(df['Lower'], df['Upper'])
print (df)
     Grp Features  Values  Lower  Upper  Evaluation
0    Men        A      10      1     10        True
1    Men        B      15      2      9       False
2    Men        C      14      2     15        True
3  Women        A       4      4      6        True
4  Women        B       6      4     10        True
5  Women        C       5      3     10        True
6   Baby        A       3      3      5        True
7   Baby        B       5      7     15       False
8   Baby        C      15      3      6       False

答案 1 :(得分:0)

您可以使用melt + crosstab + merge

m1 = df1.melt(id_vars=['Grp'])
m2 = df2.melt(id_vars='Grp')

m2[['group', 'variable']] = m2.variable.str.split('_', expand=True)

cross = pd.crosstab(index=[m2['Grp'], m2.variable], columns=m2.group,
                     values=m2['value'], aggfunc='sum').reset_index()

result = m1.merge(cross, on=['Grp', 'variable'])
result['evaluation'] = (result['value'] <= result.Upper) & (result['value'] >= result.Lower)

print(result)

输出

     Grp variable  value  Lower  Upper  evaluation
0    Men        A     10      1     10        True
1  Women        A      4      4      6        True
2   Baby        A      3      3      5        True
3    Men        B     15      2      9       False
4  Women        B      6      4     10        True
5   Baby        B      5      7     15       False
6    Men        C     14      2     15        True
7  Women        C      5      3     10        True
8   Baby        C     15      3      6       False