【问题标题】:Shift the maximum value移动最大值
【发布时间】:2019-10-18 00:32:23
【问题描述】:

很难提出一个问题。我认为屏幕截图上的一切都很清楚。

有 3 列。偏移量为 1、2、3 的值的百分比变化。 如何获得变化的最大值? 我用谷歌翻译。对不起!

enter image description here

【问题讨论】:

  • 截图看不懂。请改写问题并添加您自己的数据集和预期结果(最好没有屏幕截图以使问题独立)。
  • 是的,我同意@snwflk。在第 5 行中,max_pct_change 的值不同(shift1shift2shift3 中没有表示值)。

标签: python pandas dataframe


【解决方案1】:

我在你的桌子上看到了,要找到max_pct_change,你可以使用DataFrame.max 并尝试以下代码:

df=pd.DataFrame({'shift1':[np.nan,0.1549,-0.1012,0.0563,0.0731],
                'shift2':[np.nan,np.nan,0.0535,-0.0450,0.1295],
                'shift3':[np.nan,np.nan,np.nan,0.1098,0.0281]})
print(df)
df['max_pct_change']=df.apply(max, axis=1)
print(df)

输出:

   shift1  shift2  shift3
0     NaN     NaN     NaN
1  0.1549     NaN     NaN
2 -0.1012  0.0535     NaN
3  0.0563 -0.0450  0.1098
4  0.0731  0.1295  0.0281

   shift1  shift2  shift3  max_pct_change
0     NaN     NaN     NaN             NaN
1  0.1549     NaN     NaN          0.1549
2 -0.1012  0.0535     NaN          0.0535
3  0.0563 -0.0450  0.1098          0.1098
4  0.0731  0.1295  0.0281          0.1295

希望对你有帮助。

【讨论】:

  • 这也是我首先想到的,但是您的第 4 行的max_pct_change 与屏幕截图中的不同。此外,根据屏幕截图,第 4 行中的shift2 不应为负数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 2014-09-10
  • 2015-12-02
  • 2015-10-06
  • 2022-08-03
相关资源
最近更新 更多