Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 使用if条件获取不同列的最大值_Python_Python 3.x_Jupyter Notebook - Fatal编程技术网

Python 使用if条件获取不同列的最大值

Python 使用if条件获取不同列的最大值,python,python-3.x,jupyter-notebook,Python,Python 3.x,Jupyter Notebook,我想为每一行获取这些值的最大值

我想为每一行获取这些值的最大值<目标值

例如,从下面的数据框中,每列
date1
date2
,…
date6
将依次成为
目标值

对于每行的每个
目标值
,我希望得到小于
目标值
的最大值。如果
目标值
最小,它将返回
目标值

我有一个数据帧
df
,如下所示:

index   date1      date2      date3     date4       date5      date6 
AA     2019-8-1   2019-1-4   2019-2-3  2019-2-2    2019-5-21  2019-5-14
BB     2019-3-12  2019-10-1  2019-6-1  2019-3-17   2019-7-9   2019-6-12
CC     2019-1-11  2019-3-1   2019-8-1  2019-3-27   2019-1-11  2019-1-7
如果
目标值
date1
,我的尝试:

date1temp = []
for index, row in df.iterrows():
    mylist = ['date2','date3','date4','date5','date6']
    max = datetime.datetime(2011,1,1)
    for i in mylist:
        if row[i] < row['date1']
             if row[i] > max:
                  max = row[i]
        else:
             max = row['date1']
    date1temp.append((index,max,row['date1']))

cols = ['index','max','target']
result = pd.DataFrame(date1temp, columns=cols)
预期输出:我希望得到
结果
,如下所示:

index  max        target
AA     2019-5-21  2019-8-1
BB     2019-3-12  2019-3-12
CC     2019-1-7   2019-1-11

谢谢大家!

错误在本节中:

    max = datetime.datetime(2011,1,1)
    for i in mylist:
        if row[i] < row['date1']
             if row[i] > max:
                  max = row[i]
        else:
             max = row['date1']

首先,您必须将数据转换为datetime,以便可以按预期比较以下值:

df = df.apply(pd.to_datetime)
然后您只需使用所需的列更改
目标

target = 'date1'
target_index = df.columns.tolist().index(target)

def process(row):
    target_value = row[target_index]
    smaller = row[row < target_value]

    # check if there is any smaller
    if not smaller.empty:
        return  smaller.max()

    return target_value



pd.concat([df.agg(process, axis=1), df[target]], axis=1).rename(columns={0:'max', target: 'target'})
target='date1'
target_index=df.columns.tolist().index(目标)
def流程(世界其他地区):
目标值=行[目标索引]
较小=行[行<目标值]
#检查是否有更小的
如果不是更小。则为空:
返回较小的.max()
返回目标值
pd.concat([df.agg(进程,axis=1),df[target]],axis=1)。重命名(列={0:'max',目标:'target'})
输出:


也许你也应该包括我需要的内容。非常感谢你的帮助!我真的很感激!
df = df.apply(pd.to_datetime)
target = 'date1'
target_index = df.columns.tolist().index(target)

def process(row):
    target_value = row[target_index]
    smaller = row[row < target_value]

    # check if there is any smaller
    if not smaller.empty:
        return  smaller.max()

    return target_value



pd.concat([df.agg(process, axis=1), df[target]], axis=1).rename(columns={0:'max', target: 'target'})