Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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-将平均值应用于dataframe中的所有列,但按列分组的列除外_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python-将平均值应用于dataframe中的所有列,但按列分组的列除外

Python-将平均值应用于dataframe中的所有列,但按列分组的列除外,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有一个DF,有4个整数列“NumInstances”、“Percent”、“Value”和一个文本列“Country” 我试图为每个国家只取一行,并为该行取所有其他列的平均值 如何做到这一点 我尝试了以下方法,但无效: nsg = nsg .groupby(["Country","NumInstances","Percent"], as_index=False)['Value'].mean() nsg = nsg.gr

我有一个DF,有4个整数列“NumInstances”、“Percent”、“Value”和一个文本列“Country”

我试图为每个国家只取一行,并为该行取所有其他列的平均值

如何做到这一点

我尝试了以下方法,但无效:

    nsg = nsg .groupby(["Country","NumInstances","Percent"], as_index=False)['Value'].mean()
    nsg = nsg.groupby(["Country","Value","Percent"], as_index=False)['NumInstances'].mean()
    nsg = nsg.groupby(["Country","NumInstances","Value"], as_index=False)['Percent'].mean()


nsg.groupby(“Country”).mean()
谢谢,这导致数据框只有3个列名['Value'、'NumInstances'、'Percent']。当我稍后在脚本中尝试在绘图中调用此函数时,这会导致错误。您可以在问题中添加输入和输出示例吗?在转换之前使用:list(nsg.columns.values):['Country'、'Value'、'NumInstances'、'Percent']之后:['Value'、'NumInstances'、'Percent'](无国家)找到解决方案。我只需要添加“as_index=False”。谢谢