Python 基于dataframe中的其他三列更改一列的值

Python 基于dataframe中的其他三列更改一列的值,python,pandas,Python,Pandas,我有一个下面的数据框,我想根据“时间”、“样本”和“uid”列更改“fmc”列的值 概念如下: 对于相同的日期,如果df.samples=='C'和df.uid=='Plot1',则fmc*0.4的对应行值 同样,对于相同的日期,如果df.samples='C'和df.uid=='Plot2',则fmc*0.8的对应行值 对于相同的日期,如果df.samples==“E”和df.uid==“Plot1”,则fmc*0.4的对应行值 同样地,对于相同的日期,如果df.samples=='E'和df

我有一个下面的数据框,我想根据“时间”、“样本”和“uid”列更改“fmc”列的值

概念如下:

对于相同的
日期
如果df.samples=='C'和df.uid=='Plot1'
,则
fmc*0.4的对应行值

同样,对于相同的
日期
如果df.samples='C'和df.uid=='Plot2'
,则
fmc*0.8的对应行值

对于相同的
日期
如果df.samples==“E”和df.uid==“Plot1”
,则
fmc*0.4的对应行值

同样地,对于相同的
日期
如果df.samples=='E'和df.uid=='Plot2'
,则
fmc*0.15的对应行值

对于相同的
日期
如果df.samples=='ns'和df.uid=='Plot1'
,则
fmc*0.2的对应行值

同样,对于相同的
日期
如果df.samples=='ns'和df.uid=='Plot2'
,则
fmc*0.05的对应行值

我是python新手,所以如果我不能很好地解释,我深表歉意,如果您需要更多的澄清,请让我知道

       time        samples    uid                 fmc
0  2015-10-11        C       Plot1              98.226352
1  2015-10-11        C       Plot2             132.984817
2  2015-10-11        E       Plot1             114.147964
3  2015-10-11        E       Plot2             110.083699
4  2015-10-11        ns      Plot1             113.258977
5  2015-10-11        ns      Plot2             113.768023
6  2015-10-19        C       Plot1             118.503214
7  2015-10-19        E       Plot1             108.733209
8  2015-10-19        ns      Plot1              59.316977
9  2015-10-27        C       Plot1             104.977531
10 2015-10-27        C       Plot2             121.213887
11 2015-10-27        E       Plot1             129.575670
12 2015-10-27        E       Plot2             118.639048
13 2015-10-27        ns      Plot1             103.581065
14 2015-10-27        ns      Plot2             102.278469
15 2015-11-17        C       Plot1             103.820689
16 2015-11-17        C       Plot2             117.333382
17 2015-11-17        E       Plot1             143.418932
18 2015-11-17        E       Plot2             160.342155
19 2015-11-17        ns      Plot1              89.890484
创建查询

C1=(df.samples.eq('C')&df.uid.eq('Plot1'))|(df.samples.eq('E')&df.uid.eq('Plot1'))
C2=df.samples.eq('C')&df.uid.eq('Plot2')

C4=df.samples.eq('E')&df.uid.eq('Plot2')
C5=df.samples.eq('ns')&df.uid.eq('Plot1')
C6=C5=df.samples.eq('ns')&df.uid.eq('Plot2')
将查询放入列表中

conditions=[C1,C2,C4,C5,C6]
登记列表中每个查询对应的结果

MULT=[0.4,0.8,0.15,0.2,0.05]
创建临时列并使用np.select(条件、结果)填充

将结果与fmc相乘并删除临时列

df=df.assign(fmc=df['fmc']*df['fmc1']).drop('fmc1',1)

生成一个函数,该函数接受要素(列)并根据条件返回结果

def arrange_stuff(col2, col3, col4):
   if col2 == 'C' & col3 == 'Plot1'
      return col4*0.4
   elif ...
      return ...
然后通过应用以下功能创建新功能:

df['fmc_new'] = df(lambda x: arrange_stuff(x['samples'],x['uid'],x['fmc']), axis=1)

如果您不需要原始的fmc列,您可以简单地将其删除并重命名为新的fmc_,或者首先直接分配给它。

您应该能够使用itertuples()来处理此列,它允许您遍历数据帧的行。考虑到你对样品和uid的标准涵盖了所有日期,我不确定你所说的“同一日期”是什么意思

fmc_adjusted = []
for row in df.itertuples():
    if df.samples == 'C' and df.uid == 'Plot1':
        fmc_adjusted.append(row[4]*0.4)
    if df.samples == 'C' and df.uid == 'Plot2':
        fmc_adjusted.append(row[4]*0.15)
。。。等等,以满足不同的标准

我喜欢保留我的专栏,以防以后需要引用它们。如果要创建新列,请执行以下操作:

df['fmc_adjusted'] = fmc_adjusted
如果要替换fmc列:

df['fmc'] = fmc_adjusted
可能有更快捷、更整洁的方法,但我不知道。

此代码:

import pandas as pd
data = [
    ['2015-10-11', 'C', 'Plot1',  98.226352 ],
    ['2015-10-11', 'C', 'Plot2', 132.984817 ],
    ['2015-10-11', 'E', 'Plot1', 114.147964 ],
    ['2015-10-11', 'E', 'Plot2', 110.083699 ],
    ['2015-10-11', 'ns', 'Plot1', 113.258977 ],
    ['2015-10-11', 'ns', 'Plot2', 113.768023 ],
    ['2015-10-19', 'C', 'Plot1', 118.503214 ],
    ['2015-10-19', 'E', 'Plot1', 108.733209 ],
    ['2015-10-19', 'ns', 'Plot1',  59.316977 ],
    ['2015-10-27', 'C', 'Plot1', 104.977531 ],
    ['2015-10-27', 'C', 'Plot2', 121.213887 ],
    ['2015-10-27', 'E', 'Plot1', 129.575670 ],
    ['2015-10-27', 'E', 'Plot2', 118.639048 ],
    ['2015-10-27', 'ns', 'Plot1', 103.581065 ],
    ['2015-10-27', 'ns', 'Plot2', 102.278469 ],
    ['2015-11-17', 'C', 'Plot1', 103.820689 ],
    ['2015-11-17', 'C', 'Plot2', 117.333382 ],
    ['2015-11-17', 'E', 'Plot1', 143.418932 ],
    ['2015-11-17', 'E', 'Plot2', 160.342155 ],
    ['2015-11-17', 'ns', 'Plot1',  89.890484]
]

df = pd.DataFrame(columns=['time', 'samples', 'uid', 'fmc'], data=data)

print (df.head(10))

df['result'] = df.apply(
                lambda item:
                   (item.fmc * 0.4) if item.samples == 'C' and item.uid == 'Plot1' else \
                   (item.fmc * 0.8) if item.samples == 'C' and item.uid == 'Plot2' else \
                   (item.fmc * 0.4) if item.samples == 'E' and item.uid == 'Plot1' else \
                   (item.fmc * 0.15)if item.samples == 'E' and item.uid == 'Plot2' else \
                   (item.fmc * 0.2) if item.samples == 'ns'and item.uid == 'Plot1' else \
                   (item.fmc * 0.05)if item.samples == 'ns'and item.uid == 'Plot2' else None,
                axis=1
            )

print(df.head(10))
应产生以下输出:

         time samples    uid         fmc
0  2015-10-11       C  Plot1   98.226352
1  2015-10-11       C  Plot2  132.984817
2  2015-10-11       E  Plot1  114.147964
3  2015-10-11       E  Plot2  110.083699
4  2015-10-11      ns  Plot1  113.258977
5  2015-10-11      ns  Plot2  113.768023
6  2015-10-19       C  Plot1  118.503214
7  2015-10-19       E  Plot1  108.733209
8  2015-10-19      ns  Plot1   59.316977
9  2015-10-27       C  Plot1  104.977531
         time samples    uid         fmc      result
0  2015-10-11       C  Plot1   98.226352   39.290541
1  2015-10-11       C  Plot2  132.984817  106.387854
2  2015-10-11       E  Plot1  114.147964   45.659186
3  2015-10-11       E  Plot2  110.083699   16.512555
4  2015-10-11      ns  Plot1  113.258977   22.651795
5  2015-10-11      ns  Plot2  113.768023    5.688401
6  2015-10-19       C  Plot1  118.503214   47.401286
7  2015-10-19       E  Plot1  108.733209   43.493284
8  2015-10-19      ns  Plot1   59.316977   11.863395
9  2015-10-27       C  Plot1  104.977531   41.991012

Process finished with exit code 0
df.apply
启发,使用
axis=1
,并传递包含全套条件的lambda函数,您将在
结果
列中获得预期值

apply
函数将数据帧的列(因为
axis=1
)作为值序列中每个记录的
项传递给lambda函数。lambda函数还为序列中的每个给定记录/项返回相应的
result
值,因此我们不需要担心匹配日期/索引值


熊猫.DataFrame.apply的参考资料

到目前为止,您做了哪些工作来解决这个问题?
         time samples    uid         fmc
0  2015-10-11       C  Plot1   98.226352
1  2015-10-11       C  Plot2  132.984817
2  2015-10-11       E  Plot1  114.147964
3  2015-10-11       E  Plot2  110.083699
4  2015-10-11      ns  Plot1  113.258977
5  2015-10-11      ns  Plot2  113.768023
6  2015-10-19       C  Plot1  118.503214
7  2015-10-19       E  Plot1  108.733209
8  2015-10-19      ns  Plot1   59.316977
9  2015-10-27       C  Plot1  104.977531
         time samples    uid         fmc      result
0  2015-10-11       C  Plot1   98.226352   39.290541
1  2015-10-11       C  Plot2  132.984817  106.387854
2  2015-10-11       E  Plot1  114.147964   45.659186
3  2015-10-11       E  Plot2  110.083699   16.512555
4  2015-10-11      ns  Plot1  113.258977   22.651795
5  2015-10-11      ns  Plot2  113.768023    5.688401
6  2015-10-19       C  Plot1  118.503214   47.401286
7  2015-10-19       E  Plot1  108.733209   43.493284
8  2015-10-19      ns  Plot1   59.316977   11.863395
9  2015-10-27       C  Plot1  104.977531   41.991012

Process finished with exit code 0