Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用Panda添加列和索引以汇总值_Python_Pandas_Dataframe - Fatal编程技术网

在Python中使用Panda添加列和索引以汇总值

在Python中使用Panda添加列和索引以汇总值,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个.csv文件,在使用Panda读取它之后,我有这个输出 Year Month Brunei Darussalam ... Thailand Viet Nam Myanmar 348 2007 Jan 3813 ... 25863 12555 4887 349 2007 Feb 3471 ... 22575 1196

我有一个.csv文件,在使用Panda读取它之后,我有这个输出

     Year Month   Brunei Darussalam   ...   Thailand    Viet Nam    Myanmar 
348  2007   Jan                 3813  ...       25863       12555       4887
349  2007   Feb                 3471  ...       22575       11969       3749
350  2007   Mar                 4547  ...       33087       14060       5480
351  2007   Apr                 3265  ...       34500       15553       6838
352  2007   May                 3641  ...       30555       14995       5295
..    ...   ...                  ...  ...         ...         ...        ...
474  2017   Jul                 5625  ...       48620       71153      12619
475  2017   Aug                 4610  ...       40993       51866      10934
476  2017   Sep                 5387  ...       39692       40270       9888
477  2017   Oct                 4202  ...       61448       39013      11616
478  2017   Nov                 5258  ...       39304       36964      11402
我用它来计算总年份内所有国家的总和,以显示前三名

top3_country = new_df.iloc[0:, 2:9].sum(axis=0).sort_values(ascending=False).nlargest(3)
虽然我的输出是这样的

  Indonesia       27572424
  Malaysia        11337420
  Philippines      6548622
我想将列和索引添加到sum值中,就好像它是这样的新数据帧一样

    Countries       Visitors
  0 Indonesia       27572424
  1 Malaysia        11337420
  2 Philippines      6548622
抱歉,我刚刚开始学习Panda,非常感谢您提供的任何帮助

用于2列
DataFrame
,然后从列表中设置新列名称:

top3_country = top3_country.reset_index()
top3_country.columns = ['Countries', 'Visitors']
或用于:


您可以返回
pd.DataFrame
,使用
reset\u index
rename
。将代码更改为:

import pandas as pd
top3_country = pd.DataFrame(df.iloc[0:, 2:9].sum(axis=0).sort_values(ascending=False).nlargest(3)
                            ).reset_index(
                                ).rename(columns={'index':'Countries',0:'visitors'})
top3_country

  Countries  visitors
0  Indonesia   27572424
1  Malaysia    11337420
2  Philippines  6548622

欢迎来到Stackoverflow。请花点时间阅读这篇文章,以及如何提供答案,并相应地修改你的问题。这些提示可能也很有用。是一个关于如何提问的好资源。我们喜欢数据不在图像中,我们真的鼓励人们在提问之前尝试一些代码,这样你就可以展示一些作品。我会记住在下一个问题中不要使用图像。对不起,这是我在hub ty上提出的第一个问题,以获得建议
import pandas as pd
top3_country = pd.DataFrame(df.iloc[0:, 2:9].sum(axis=0).sort_values(ascending=False).nlargest(3)
                            ).reset_index(
                                ).rename(columns={'index':'Countries',0:'visitors'})
top3_country

  Countries  visitors
0  Indonesia   27572424
1  Malaysia    11337420
2  Philippines  6548622