Pandas在一排不同的列中发现了增加的趋势

2024-05-16 13:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个印度牛奶生产的数据集。我试图得到5个州的名单(如果有的话),在过去的3年里,通过大熊猫产奶量增加了。在

State        Total10-11 Total11-12  Total13-14  Total14-15  Total15-16
Andhra Pradesh    11204      12088       13007         9656      10817
Arunachal Pradesh    28         22          43           46         50
Assam               790        797         814          829        844
Bihar              6517       6643        7197         7775       8289
Chhattisgarh       1030       1118        1208         1232       1278
Goa                  60         60          68           66         54
Gujarat            9322       4089       11112        11690      12262
Haryana            6268       6661        7442         7902       8381
Himachal Pradesh   1102       1120        1151         1173       1283

预期产量:

^{pr2}$

我想找出每年牛奶产量呈上升趋势的州。与前几年相比,接下来几年的牛奶产量不应下降。预期产出状态的产量是按递增顺序排列的,而且产量甚至一次也没有下降。我有点困在这个问题上,我尝试了一些方法,但它们都不接近正确的答案。解决办法是什么?提前谢谢。在


Tags: 数据state产量大熊猫牛奶名单andhrapradesh
2条回答

如果你只是在寻找一种趋势,那么我认为可视化就是答案。在

你可以这样做。在

import matplotlib.pyplot as plt
import pandas as pd

df = df.set_index('state')
df.T.plot(figsize=(10,15))

enter image description here

或者单独来看:

^{pr2}$

enter image description here

如果您寻找的差异总是在增加,您可以使用diff > 0和{},即

df = df.set_index("State/UT Name")

temp = (df.T.diff() > 0).cumsum()
# Values will increment if the difference between past and present is positive 
State/UT Name  Andhra Pradesh  Arunachal Pradesh  Assam  Bihar  Chhattisgarh  \
Total10-11                  0                  0      0      0             0   
Total11-12                  1                  0      1      1             1   
Total13-14                  2                  1      2      2             2   
Total14-15                  2                  2      3      3             3   
Total15-16                  3                  3      4      4             4   

State/UT Name  Goa  Gujarat  Haryana  Himachal Pradesh  
Total10-11       0        0        0                 0  
Total11-12       0        0        1                 1  
Total13-14       1        1        2                 2  
Total14-15       1        2        3                 3  
Total15-16       1        3        4                 4  

# The one with max sum is the one that kept increasing over time 
temp.sum().nlargest(10)

State/UT Name
Assam                10
Bihar                10
Chhattisgarh         10
Haryana              10
Himachal Pradesh     10
Andhra Pradesh        8
Arunachal Pradesh     6
Gujarat               6
Goa                   3

如果你想知道州名的话

^{pr2}$

相关问题 更多 >