Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为特定索引的dataframe分配具有相同索引的dataframe值_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何为特定索引的dataframe分配具有相同索引的dataframe值

Python 如何为特定索引的dataframe分配具有相同索引的dataframe值,python,pandas,dataframe,Python,Pandas,Dataframe,假设我有 | channel | sum | txn | value | count | group 0 | A | null | null | 2 | 1 | 1 1 | A | null | null | 3 | 3 | 1 2 | B | null | null | 4 | 4 | 2 3 | C | null | nu

假设我有

    | channel |   sum   |  txn  | value | count | group
0   |    A    |  null   |  null |   2   |   1   |  1
1   |    A    |  null   |  null |   3   |   3   |  1 
2   |    B    |  null   |  null |   4   |   4   |  2
3   |    C    |  null   |  null |   2   |   2   |  2
4   |    A    |  null   |  null |   1   |   5   |  1
当我使用

df.loc[df['group']==1,['sum','txn']=df.loc[df['group']==1].groupby(['channel'])['value','count'].apply(λx:x+1)

它没有给我的数据框赋值

应该是这样的

    | channel |   sum   |  txn  | value | count | group
0   |    A    |    3    |   2   |   2   |   1   |  1
1   |    A    |    4    |   4   |   3   |   3   |  1 
2   |    B    |  null   |  null |   4   |   4   |  2
3   |    C    |  null   |  null |   2   |   2   |  2
4   |    A    |    2    |   6   |   1   |   5   |  1

这里的值不是每个组的计数,因此应通过省略
groupby
简化解决方案,因为需要正确对齐的值,所以需要将输出转换为numpy数组:

m = df['group'] == 1
df.loc[m ,['sum','txn']] = (df.loc[m, ['value','count']] + 1).to_numpy()
#oldier pandas versions
#df.loc[m ,['sum','txn']] = (df.loc[m, ['value','count']] + 1).values
print (df)
  channel  sum  txn  value  count  group
0       A  3.0  2.0      2      1      1
1       A  4.0  4.0      3      3      1
2       B  NaN  NaN      4      4      2
3       C  NaN  NaN      2      2      2
4       A  2.0  6.0      1      5      1
编辑:对于每个组的规格化,可以使用:


谢谢,但如果我需要使用通道作为一个组来规范化数据呢<代码>x-x.max()/x.max()-x.min()
m = df['group'] == 1
df.loc[m ,['sum','txn']] = (df[m].groupby('channel')['value','count']
                                 .transform(lambda x: x - x.max() / x.max() - x.min())
                                 .to_numpy())

print (df)
  channel  sum  txn  value  count  group
0       A  0.0 -1.0      2      1      1
1       A  1.0  1.0      3      3      1
2       B  NaN  NaN      4      4      2
3       C  NaN  NaN      2      2      2
4       A -1.0  3.0      1      5      1