首页 文章

将新列附加到groupby对象中的pandas数据帧

提问于
浏览
0

我想将列添加到groupby对象中的pandas数据帧

# create the dataframe
idx  =  ['a','b','c'] * 10

df = pd.DataFrame({
               'f1'   : np.random.randn(30), 
               'f2'   : np.random.randn(30),
               'f3'   : np.random.randn(30),
               'f4'   : np.random.randn(30),
               'f5'   : np.random.randn(30)},
                index = idx)

colnum         = [1,2,3,4,5]

newcol         = ['a' + str(s)  for s in colnum]


# group by the index
df1             = df.groupby(df.index)

尝试遍历groupby对象中的每个组,并将新列添加到组中的当前数据框

for group in df1:
   tmp = group[1]
   for s in range(len(tmp.columns)):    
        print(s)
        tmp.loc[:,newcol[s]] = tmp[[tmp.columns[s]]]  * colnum[s]
        group[1]  = tmp

我无法将新数据帧添加到组对象

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

TypeError: 'tuple' object does not support item assignment

有没有办法用新数据帧替换groupby对象中的数据帧?

1 回答

  • 1

    根据你的代码:(PS: df.mul([1,2,3,4,5]) 为你工作的例子)

    grouplist=[]
    for _,group in df1:
       tmp = group
       for s in range(len(tmp.columns)):
            print(s)
            tmp.loc[:,newcol[s]] = tmp[[tmp.columns[s]]]  * colnum[s]
       grouplist.append(tmp)
    
    
    grouplist[1]
    Out[217]: 
             f1        f2        f3        f4        f5        a1        a2  \
    b -0.262064 -1.148832 -1.835077 -0.244675 -0.215145 -0.262064 -2.297664   
    b -1.595659 -0.448111 -0.908683 -0.157839  0.208497 -1.595659 -0.896222   
    b  0.373039 -0.557571  1.154175 -0.172326  1.236915  0.373039 -1.115142   
    b -1.485564  1.508292  0.420220 -0.380387 -0.725848 -1.485564  3.016584   
    b -0.760250 -0.380997 -0.774745 -0.853975  0.041411 -0.760250 -0.761994   
    b  0.600410  1.822984 -0.310327 -0.281853  0.458621  0.600410  3.645968   
    b -0.707724  1.706709 -0.208969 -1.696045 -1.644065 -0.707724  3.413417   
    b -0.892057  1.225944 -1.027265 -1.519110 -0.861458 -0.892057  2.451888   
    b -0.454419 -1.989300  2.241945 -1.071738 -0.905364 -0.454419 -3.978601   
    b  1.171569 -0.827023 -0.404192 -1.495059  0.500045  1.171569 -1.654046   
             a3        a4        a5  
    b -5.505230 -0.978700 -1.075727  
    b -2.726048 -0.631355  1.042483  
    b  3.462526 -0.689306  6.184576  
    b  1.260661 -1.521547 -3.629239  
    b -2.324236 -3.415901  0.207056  
    b -0.930980 -1.127412  2.293105  
    b -0.626908 -6.784181 -8.220324  
    b -3.081796 -6.076439 -4.307289  
    b  6.725834 -4.286954 -4.526821  
    b -1.212577 -5.980235  2.500226
    

相关问题