我有一个名为 Growth 的数据框,有 4 列。
State Name Average Fare ($)_x Average Fare ($)_y Average Fare ($)
0 AK 599.372368 577.790640 585.944324
1 AL 548.825867 545.144447 555.939466
2 AR 496.033146 511.867026 513.761296
3 AZ 324.641818 396.895324 389.545267
4 CA 368.937971 376.723839 366.918761
5 CO 502.611572 537.206439 531.191893
6 CT 394.105453 388.772428 370.904182
7 DC 390.872738 382.326510 392.394165
8 FL 324.941100 329.728524 337.249248
9 GA 485.335737 480.606365 489.574241
10 HI 326.084793 335.547369 298.709998
11 IA 428.151682 445.625840 462.614195
12 ID 482.092567 475.822275 491.714945
13 IL 329.449503 349.938794 346.022226
14 IN 391.627917 418.945137 412.242053
15 KS 452.312058 490.024059 420.182836
最后三列是每个州每年的平均票价。 第2、3、4列分别是2017、2018、2019年。 我想知道自 2017 年以来哪个州的票价涨幅最高。
我尝试使用我的这段代码,它给出了一些我无法真正理解的输出。 我只需要找到自 2017 年以来票价涨幅最高的州。
我的代码:
growth[['Average Fare ($)_x','Average Fare ($)_y','Average Fare ($)']].pct_change()
最佳答案
你可以这样
df.set_index('State_name').pct_change(periods = 1, axis='columns').idxmax()
如果您想计算第一年和第三年之间的差异,请将periods
值更改为2。
输出
Average_fare_x NaN
Average_fare_y AZ #state with max change between 1st & 2nd year
Average_fare WV #state with max change between 2nd & 3rd year
关于python - 使用 python pandas 找到最高的增长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286715/