Python Dataframe-获取特定数字与列值之间的最大值

Python Dataframe-获取特定数字与列值之间的最大值,python,python-3.x,pandas,dataframe,max,Python,Python 3.x,Pandas,Dataframe,Max,当我有一个低于df的值时,我想得到一个列'C',它的最大值在特定值'15'和列'a'之间,条件为“B='t'” 我试过这个: testdf.loc[testdf['B']=='t', 'C'] = max(15,(testdf.loc[testdf['B']=='t','A'])) 期望输出为: A B C 0 20 t 20 1 16 t 16 2 7 t 15 3 3 t 15 4 8 f 8 你能帮我得到输出吗?

当我有一个低于df的值时,我想得到一个列'C',它的最大值在特定值'15'和列'a'之间,条件为“B='t'”

我试过这个:

testdf.loc[testdf['B']=='t', 'C'] = max(15,(testdf.loc[testdf['B']=='t','A']))
期望输出为:

    A   B   C
0   20  t   20
1   16  t   16
2   7   t   15
3   3   t   15 
4   8   f   8

你能帮我得到输出吗?谢谢大家!

使用
np.where
clip

testdf['C'] = np.where(testdf['B'].eq('t'), 
                       testdf['A'].clip(15), df['A'])
或与
系列类似。其中

testdf['C'] = (testdf['A'].clip(15)
                   .where(testdf['B'].eq('t'), testdf['A'])
              )
输出:

    A  B   C
0  20  t  20
1  16  t  16
2   7  t  15
3   3  t  15
4   8  f   8

您还可以使用
update
方法:

testdf['C'] = testdf['A']

    A  B   C
0  20  t  20
1  16  t  16
2   7  t   7
3   3  t   3
4   8  f   8


values = testdf.A[testdf.B.eq('t')].clip(15)

values
Out[16]: 
0    20
1    16
2    15
3    15
Name: A, dtype: int64

testdf.update(values.rename('C'))

    A  B     C
0  20  t  20.0
1  16  t  16.0
2   7  t  15.0
3   3  t  15.0
4   8  f   8.0

要将任何公式应用于数据帧中的单个值,可以使用

df['column']=df['column'].apply(lambda x:anyFunc(x))


这里的x将一个接一个地捕获列的各个值,并将其传递给函数,您可以在函数中对其进行操作并返回。

@SayandipDutta是的,即将使用
系列进行更新。where
。非常感谢!我这样想:
np.max(np.where(testdf.B='t',15,testdf.A),testdf.A)
testdf['C'] = testdf['A']

    A  B   C
0  20  t  20
1  16  t  16
2   7  t   7
3   3  t   3
4   8  f   8


values = testdf.A[testdf.B.eq('t')].clip(15)

values
Out[16]: 
0    20
1    16
2    15
3    15
Name: A, dtype: int64

testdf.update(values.rename('C'))

    A  B     C
0  20  t  20.0
1  16  t  16.0
2   7  t  15.0
3   3  t  15.0
4   8  f   8.0