将值添加到基于其他两列的新列
Adding values to a new column based on other two columns
我有一个看起来像这样的数据框。我创建了 3 个新列,它们将从其他列中获取值 columns.I 希望功能列单独列并为每个用户获取每个功能的总小时数。
User Function Total hours Damage Processing problem solve damages sweeper
schae Damage Processing 9.36
Julie Problem solve 9.70
John sweeper 18.9
Dan Damages 1.83
Dan Damages 1.83
Julie Damages 1.83
Dan Problem solve 1.83
预期的输出看起来像
User Function Total hours Damage Processing problem solve damages sweeper
schae Damage Processing 9.36 9.36
Julie Problem solve 9.70 9.70
John sweeper 18.9 18.9
Dan Damages 1.83 1.83
Dan sweeper 1.83 1.83
Julie Damages 1.83 1.83
Dan Problem solve 1.83 1.83
我想到了 pd.melt 但它抛出一个错误值 var not present
res = pd.melt(result,id_vars = ['Function'],value_vars=['Total hours'])
这里有一个使用 get_dummies
and df.assign
的方法:
out = (df[['User','Function','Total hours']].assign(**pd.get_dummies(df['Function'])
.mul(df['Total hours'],axis=0).replace(0,np.nan)))
print(out)
User Function Total hours Damage Processing Damages \
0 schae Damage Processing 9.36 9.36 NaN
1 Julie Problem solve 9.70 NaN NaN
2 John sweeper 18.90 NaN NaN
3 Dan Damages 1.83 NaN 1.83
4 Dan Damages 1.83 NaN 1.83
5 Julie Damages 1.83 NaN 1.83
6 Dan Problem solve 1.83 NaN NaN
Problem solve sweeper
0 NaN NaN
1 9.70 NaN
2 NaN 18.9
3 NaN NaN
4 NaN NaN
5 NaN NaN
6 1.83 NaN
for i in range(len(df)):
col = df.loc[i]['Function']
df.at[i, col] = df.xs(i)['Total hours']
print(col)
试试这个!
变量 col
查找要为其插入值 Total hours
的列。
我有一个看起来像这样的数据框。我创建了 3 个新列,它们将从其他列中获取值 columns.I 希望功能列单独列并为每个用户获取每个功能的总小时数。
User Function Total hours Damage Processing problem solve damages sweeper
schae Damage Processing 9.36
Julie Problem solve 9.70
John sweeper 18.9
Dan Damages 1.83
Dan Damages 1.83
Julie Damages 1.83
Dan Problem solve 1.83
预期的输出看起来像
User Function Total hours Damage Processing problem solve damages sweeper
schae Damage Processing 9.36 9.36
Julie Problem solve 9.70 9.70
John sweeper 18.9 18.9
Dan Damages 1.83 1.83
Dan sweeper 1.83 1.83
Julie Damages 1.83 1.83
Dan Problem solve 1.83 1.83
我想到了 pd.melt 但它抛出一个错误值 var not present
res = pd.melt(result,id_vars = ['Function'],value_vars=['Total hours'])
这里有一个使用 get_dummies
and df.assign
的方法:
out = (df[['User','Function','Total hours']].assign(**pd.get_dummies(df['Function'])
.mul(df['Total hours'],axis=0).replace(0,np.nan)))
print(out)
User Function Total hours Damage Processing Damages \
0 schae Damage Processing 9.36 9.36 NaN
1 Julie Problem solve 9.70 NaN NaN
2 John sweeper 18.90 NaN NaN
3 Dan Damages 1.83 NaN 1.83
4 Dan Damages 1.83 NaN 1.83
5 Julie Damages 1.83 NaN 1.83
6 Dan Problem solve 1.83 NaN NaN
Problem solve sweeper
0 NaN NaN
1 9.70 NaN
2 NaN 18.9
3 NaN NaN
4 NaN NaN
5 NaN NaN
6 1.83 NaN
for i in range(len(df)):
col = df.loc[i]['Function']
df.at[i, col] = df.xs(i)['Total hours']
print(col)
试试这个!
变量 col
查找要为其插入值 Total hours
的列。