我有一个很长的数据集,想连续获取最小值/最大值:
County Year1 Year2 Year3 Year4
1 Autauga 54660 55253 51253 56253
输出应该是这样的
County Year1 Year2 Year3 Year4 Min Max Max-Min
1 Autauga 54660 55253 51253 56253 51253 56253 5000
我的第一个镜头产生了一个字符串作为最大值(我已经在论坛上阅读了所有原因):
df['Max'] = df.max(axis=1)
1) 如何排除我的第一列以便 max 函数正确运行(我的输出中仍然需要县)?
2) 如何运行最大、最小函数并一次计算每个值的差值?
干杯,
请您参考如下方法:
您还可以指定您只想对数字元素执行此操作。
df['Max'] = df.max(axis=1, numeric_only=True)
df['Min'] = df.min(axis=1, numeric_only=True)
df['Max - Min'] = df['Max'] - df['Min']
# if you only need "Max - Min"
df['Max - Min'] = df.max(1, numeric_only=True) - df.min(1, numeric_only=True)
默认情况下,max 函数将尝试使用所有内容。使用额外的参数,它将只包括计算中包含 float 、整数和 bool 值的列。在 DataFrame.max 阅读更多相关信息
