Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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
如何找到列的最大值并使用pandas按顺序排列?_Pandas - Fatal编程技术网

如何找到列的最大值并使用pandas按顺序排列?

如何找到列的最大值并使用pandas按顺序排列?,pandas,Pandas,我有以下数据帧 import pandas as pd import numpy as np d = { 'ID':[1,2,3,4,5,6], 'Price1':[5,9,4,3,9,np.nan], 'Price2':[9,10,13,14,18,np.nan], 'Price3':[5,9,4,3,9,np.nan], 'Price4':[9,10,13,14,18,np.nan], 'Price5':[5,9,4,3,9,np.nan],

我有以下数据帧

import pandas as pd
import numpy as np
d = {

    'ID':[1,2,3,4,5,6],
    'Price1':[5,9,4,3,9,np.nan],
    'Price2':[9,10,13,14,18,np.nan],
    'Price3':[5,9,4,3,9,np.nan],
    'Price4':[9,10,13,14,18,np.nan],
    'Price5':[5,9,4,3,9,np.nan],
    'Price6':[np.nan,10,13,14,18,np.nan],
    'Price7':[np.nan,9,4,3,9,np.nan],
    'Price8':[np.nan,10,13,14,18,np.nan],
    'Price9':[5,9,4,3,9,np.nan],
    'Price10':[9,10,13,14,18,np.nan],
     'Type':['A','A','B','C','D','D'],


}
df = pd.DataFrame(data = d)
df
如何在价格1到价格10之间找到最大值并将其添加为新列

以下数据帧是预期的输出

import pandas as pd
import numpy as np
d = {

    'ID':[1,2,3,4,5,6],
    'Price1':[5,9,4,3,9,np.nan],
    'Price2':[9,10,13,14,18,np.nan],
    'Price3':[5,9,4,3,9,np.nan],
    'Price4':[9,10,13,14,18,np.nan],
    'Price5':[5,9,4,3,9,np.nan],
    'Price6':[np.nan,10,13,14,18,np.nan],
    'Price7':[np.nan,9,4,3,9,np.nan],
    'Price8':[np.nan,10,13,14,18,np.nan],
    'Price9':[5,9,4,3,9,np.nan],
    'Price10':[9,10,13,14,18,np.nan],
     'Type':['A','A','B','C','D','D'],
    'first_max':[9,10,13,14,18,np.nan],
    'two_max':[9,10,13,14,18,np.nan],
    'three_max':[9,10,13,14,18,np.nan],
    'four_max':[5,10,13,14,18,np.nan],
    'five_max':[5,10,13,14,18,np.nan],
    'six_max':[5,5,4,3,9,np.nan],
    'seven_max':[5,5,4,3,9,np.nan],
    'eight_max':[np.nan,5,4,3,9,np.nan],
    'nine_max':[np.nan,5,4,3,9,np.nan],
    'ten_max':[np.nan,5,4,3,9,np.nan],


}
df = pd.DataFrame(data = d)
pd.set_option('max_columns',25)
df
如何在价格1到价格10之间找到最大值,并将其添加为新列?

使用此选项:

m=df.filter(like='Price') #filter only Price columns
cols=['max'+str(i+1) for i in range(len(m.columns))]
#['max1', 'max2', 'max3', 'max4', 'max5', 'max6', 'max7', 'max8', 'max9', 'max10']
然后使用我们按降序对每一行进行排序,在此之后我们创建一个数据帧,并在轴=1上进行concat

df_new=pd.DataFrame(abs(np.sort(-m,axis=1)),columns=cols)
df1=pd.concat([df,df_new],axis=1)
print(df1)


@熊猫补充了一些解释。希望它能帮助
np中的
-m
。排序
使其下降?我不知道。伟大的answer@Erfan
-
将使数字为负数,
-2
小于
-1
,因此
np.sort()
将把
-2
放在第一位。然后我用abs()处理同样的事情:)谢谢you@anky_91你可以使用argsort,samething:-)@anky_91 opps,我想我误解了这里的问题排序应该是好的
ID  Price1  Price2  Price3  Price4  Price5  Price6  Price7  Price8  Price9  \
0   1     5.0     9.0     5.0     9.0     5.0     NaN     NaN     NaN     5.0   
1   2     9.0    10.0     9.0    10.0     9.0    10.0     9.0    10.0     9.0   
2   3     4.0    13.0     4.0    13.0     4.0    13.0     4.0    13.0     4.0   
3   4     3.0    14.0     3.0    14.0     3.0    14.0     3.0    14.0     3.0   
4   5     9.0    18.0     9.0    18.0     9.0    18.0     9.0    18.0     9.0   
5   6     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN   

   ...  max1  max2  max3  max4  max5  max6  max7  max8  max9  max10  
0  ...   9.0   9.0   9.0   5.0   5.0   5.0   5.0   NaN   NaN    NaN  
1  ...  10.0  10.0  10.0  10.0  10.0   9.0   9.0   9.0   9.0    9.0  
2  ...  13.0  13.0  13.0  13.0  13.0   4.0   4.0   4.0   4.0    4.0  
3  ...  14.0  14.0  14.0  14.0  14.0   3.0   3.0   3.0   3.0    3.0  
4  ...  18.0  18.0  18.0  18.0  18.0   9.0   9.0   9.0   9.0    9.0  
5  ...   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN    NaN