参考熔体变量列以计算数据范围中的另一列

时间:2018-10-16 09:17:13

标签: python pandas melt

我使用python / pandas使用了melt()函数来转换数据

Person  Score1  Score2  V1  V2
A   1   4   6   8
B   2   5   3   6
C   3   6   4   7

放入表格

 Person variable  value  V1  V2
0      A   Score1      1   6  8
1      B   Score1      2   3  6
2      C   Score1      3   4  7
3      A   Score2      4   6  8
4      B   Score2      5   3  6
5      C   Score2      6   4  7

我现在想在其中添加另一列V

  

如果变量= Score1,则V = V1;如果变量= Score2,则= V2

导致:

  Person variable  value  V
0      A   Score1      1  6
1      B   Score1      2  3
2      C   Score1      3  4
3      A   Score2      4  8
4      B   Score2      5  6
5      C   Score2      6  7

我尝试使用var_name来命名变量属性,但是它似乎并没有真正定义它,因此很难用它来计算V列的值,有什么想法吗?

2 个答案:

答案 0 :(得分:2)

使用wide_to_long

LazyDivideResult::remainder()

答案 1 :(得分:0)

使用int temp1 = Stream.of(st1) .filter(s -> !s.isEmpty()) .mapToInt(Integer::new) .sum();

np.where