Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Series.name must be a hashable type

    df = pd.DataFrame(
{
    "Index1": ["A", "A", "B", "B", "C"],
    "Index2": ["a", "b", "a", "c", "a"],
    "Param1": [1, 3, 1, 4, 2],
    "Param2": [2, 4, 3, 3, 4],
})

I wanna try to df.groupby(["Index1","Index2"])["Param1","Param2"].apply(lambda x:x['Param1']/x['Param2']),but got an error infomatation like that:TypeError: Series.name must be a hashable type, How should I fix it,thank you for your help.

I wanna generate data like below enter image description here

Index2  Index1  a   b   c
0   A   0.50    0.75    nan
1   B   0.33    nan 1.33
2   C   0.50    nan nan

this format is I use df.assign(dis=lambda x:x.Param1/x.Param2).groupby(['Index1','Index2'])['dis'].apply(float).unstack().reset_index() generate,I wanna to know .apply(float) the way if is very dangerous

like image 249
Aaron Hu Avatar asked Sep 20 '25 11:09

Aaron Hu


1 Answers

I think groupby here is not necessary, only divide columns:

df['new'] = df['Param1']/df['Param2']
print (df)
  Index1 Index2  Param1  Param2       new
0      A      a       1       2  0.500000
1      A      b       3       4  0.750000
2      B      a       1       3  0.333333
3      B      c       4       3  1.333333
4      C      a       2       4  0.500000

And then use DataFrame.pivot:

df = df.pivot('Index1','Index2','new')
print (df)
Index2         a     b         c
Index1                          
A       0.500000  0.75       NaN
B       0.333333   NaN  1.333333
C       0.500000   NaN       NaN

But your solution is possible with Series.to_frame:

df1 = (df.groupby(["Index1","Index2"])["Param1","Param2"]
         .apply(lambda x:(x['Param1']/x['Param2']).to_frame('new')))
print (df1)
        new
0  0.500000
1  0.750000
2  0.333333
3  1.333333
4  0.500000
like image 118
jezrael Avatar answered Sep 23 '25 10:09

jezrael