找到差异。 groupby在熊猫中的最大和最小数?

时间:2020-06-27 10:10:25

标签: python max pandas-groupby min difference

如果我具有三个站2015年至2017年降雨3年以下的日期范围,您能帮我如何找到差异。每个站的最大值和最小值之间? enter image description here

1 个答案:

答案 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

enter image description here

选项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

enter image description here