【问题标题】:how can add trend arrow in python pandas?如何在 python pandas 中添加趋势箭头?
【发布时间】:2020-01-27 07:20:30
【问题描述】:

我有一个 pandas 的数据框,其中包含一些团队和他们多年来的得分。数据是这样的:

df = pd.DataFrame({'Team': ['A', 'B', 'C', 'D'],
              '2016' : [16, 13, 15, 19],
              '2017' : [15, 16, 14, 19],
              '2018' : [13, 17, 14, 17],
              '2019' : [15, 15, 16, 19]
             })

我想计算每个团队每年的排名,然后跟踪团队每年排名的变化。 我想在熊猫中用上下箭头显示排名的变化。

结果必须是这样的:

【问题讨论】:

标签: python pandas plot trend


【解决方案1】:

您可以在axis=1 上使用df.rankdf.diff(),然后使用np.select 和条件和unicodes

m = df.set_index('Team') #set Team as index
n = m.rank(ascending=False,method='min').add_suffix('_rank') #get rank
a = n.diff(axis=1).iloc[:,1:].add_suffix('_trned') #take difference on axis=1
a[:] = np.select([a.lt(0),a.gt(0),a.eq(0)],[u"\u2193",u"\u2191",u"\u2192"])

final = pd.concat((m,n,a),axis=1).reset_index()

  Team  2016  2017  2018  2019  2016_rank  2017_rank  2018_rank  2019_rank  \
0    A    16    15    13    15        2.0        3.0        4.0        3.0   
1    B    13    16    17    15        4.0        2.0        1.0        3.0   
2    C    15    14    14    16        3.0        4.0        3.0        2.0   
3    D    19    19    17    19        1.0        1.0        1.0        1.0   

  2017_rank_trned 2018_rank_trned 2019_rank_trned  
0               ↑               ↑               ↓  
1               ↓               ↓               ↑  
2               ↑               ↓               ↓  
3               →               →               →

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2021-05-31
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多