Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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 熊猫,如何添加具有平均分组列的行_Python_Pandas - Fatal编程技术网

Python 熊猫,如何添加具有平均分组列的行

Python 熊猫,如何添加具有平均分组列的行,python,pandas,Python,Pandas,我有一个如下所示的数据帧。 我想为每个水果添加一行,其中 新行的价格应设置为该水果预先存在的行的平均价格。 新行的资源将始终为“全部”。 新行的ftype将始终为avg。 我知道如何生成一个新的列,显示每个水果的平均价格,但我不知道如何用这个平均值添加一行。 你能帮我吗 import numpy as np import pandas as pd fruit = ['apple','apple','banana','banana','kiwi','kiwi','grape','grape'] f

我有一个如下所示的数据帧。 我想为每个水果添加一行,其中

新行的价格应设置为该水果预先存在的行的平均价格。 新行的资源将始终为“全部”。 新行的ftype将始终为avg。 我知道如何生成一个新的列,显示每个水果的平均价格,但我不知道如何用这个平均值添加一行。 你能帮我吗

import numpy as np
import pandas as pd
fruit = ['apple','apple','banana','banana','kiwi','kiwi','grape','grape']
ftype = ['one','two','one','two','three','one','one','two']
resource = ['us','us','us','us','us','us','us','us']
price = [100,150,200,300,120,300,400,500]
df = pd.DataFrame({'fruit':fruit,'ftype':ftype,'resource':resource,'price':price})
print(df)
原始数据帧:


    fruit  ftype  price resource
0   apple    one    100       us
1   apple    two    150       us
2  banana    one    200       us
3  banana    two    300       us
4    kiwi  three    120       us
5    kiwi    one    300       us
6   grape    one    400       us
7   grape    two    500       us
我想要生成的内容:

    fruit  ftype  price resource
0   apple    one    100       us
1   apple    two    150       us
    apple    avg    125      all
2  banana    one    200       us
3  banana    two    300       us
   banana    avg    250       all
4    kiwi  three    120       us
5    kiwi    one    300       us
     kiwi    avg    210       all
6   grape    one    400       us
7   grape    two    500       us
    grape    avg    450       all
您可以使用以下内容聚合平均值并添加新列:

然后使用和排序值:

df = (pd.concat([df, df1], sort=True)
        .sort_values(['fruit','resource'], ascending=[True, False])
        .reset_index(drop=True))
print (df)
     fruit  ftype  price resource
0    apple    one    100       us
1    apple    two    150       us
2    apple    avg    125      all
3   banana    one    200       us
4   banana    two    300       us
5   banana    avg    250      all
6    grape    one    400       us
7    grape    two    500       us
8    grape    avg    450      all
9     kiwi  three    120       us
10    kiwi    one    300       us
11    kiwi    avg    210      all

哇!谢谢你的快速回复。。我试过了,效果很好谢谢:@dideod98,记住点击复选标记接受这个答案,如果你还没有找到任何有用的答案,请向上投票:-
df = (pd.concat([df, df1], sort=True)
        .sort_values(['fruit','resource'], ascending=[True, False])
        .reset_index(drop=True))
print (df)
     fruit  ftype  price resource
0    apple    one    100       us
1    apple    two    150       us
2    apple    avg    125      all
3   banana    one    200       us
4   banana    two    300       us
5   banana    avg    250      all
6    grape    one    400       us
7    grape    two    500       us
8    grape    avg    450      all
9     kiwi  three    120       us
10    kiwi    one    300       us
11    kiwi    avg    210      all