答案 0 :(得分:0)
下面的代码将groupby()
与axis=1
结合使用,以获取每一行的min()
和max()
。然后使用.merge()
将这些restul合并:
选项1 :
在“名称”列中使用非重复名称
# Import libraries
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'Name':['Baghdad', 'Basra', 'Mousl'],
'R2015':[300,190,350],
'R2016':[240,180,540],
'R2017':[290,160,490]
})
# Convert column to index
df = df.set_index('Name')
# Get min and max
df_min = df.groupby(['min']*df.shape[1],axis=1).min()
df_max = df.groupby(['max']*df.shape[1],axis=1).max()
# Combine
df_min_max = df_min.merge(df_max, on='Name')
# Get difference
df_min_max['diff'] = abs(df_min_max['min'] - df_min_max['max'])
# Output
df_min_max
选项2 :
如果DataFrame在第Name
列中有重复的名称,则下面的内容应该起作用。在这里,将Baghdad
添加为附加的重复行。这里,使用了groupby()
中的groupby()
。
# Import libraries
import pandas as pd
# Create DataFrame
df = pd.DataFrame({
'Name':['Baghdad', 'Basra', 'Mousl','Baghdad'],
'R2015':[300,190,350,780],
'R2016':[240,180,540,455],
'R2017':[290,160,490,23]
})
# Convert column to index
df = df.set_index('Name')
# Get min and max
df_min = df.groupby(['min']*df.shape[1],axis=1).min().groupby(['Name']).min()
df_max = df.groupby(['max']*df.shape[1],axis=1).max().groupby(['Name']).max()
# Combine
df_min_max = df_min.merge(df_max, on='Name')
# Get difference
df_min_max['diff'] = abs(df_min_max['min'] - df_min_max['max'])
# Output
df_min_max