0

I am looking forward to groupby the columns in a df and I had to look to this post pandas groupby dropping columns because in my case I am also loosing columns when group_by().mean() and I tried to do a

df.groupby("A", as_index=False).mean()

and a

df.groupby('A').mean().reset_index()

Then I checked the columns in my df and found out 3 of the columns are object type with

df.types

The issue i have is that i am unable to change the columns type from object to float64 ( which are by the way the columns that dissapear after the groupby

what i tried to change my columns is:

df['A']=df['A'].astype(float)

df['A']=df['A'].astype(np.float64)

df.convert_objects(convert_numeric=True)

pd.to_numeric(df, errors='coerce')

But did not worked either

But the columns continue to be object type.

It is complex to replicate my df with the dtypes of the columns, but I will post the df used for this case.

My df:

df=pd.DataFrame(data=np.transpose([[1.014e-7,0,3,1.014e-7],[2,4,6,8],[1,1,1,1],[5,5,5,8]]),index=['x','y','w','z'],columns=['A','B','C','D'])
JamesHudson81
  • 2,215
  • 4
  • 23
  • 42
  • What's the result of printing `df.dtypes` after applying `df = df.apply(np.float64)`? – Ted Aug 15 '19 at 11:02

2 Answers2

1

Your code actually works fine for me (Python version 3.6). Try checking your Python version:

import sys
print(sys.version)    # Python 3.6.2 

If converting all columns to floats is the problem, try and use:

df = df.apply(np.float64)
df.dtypes

You should end up with:

A    float64
B    float64
C    float64
D    float64
Ted
  • 1,189
  • 8
  • 15
  • @ge00rge Yes I only realised you mentioned it was too complex to replicate the df after I posted. Does the `apply` function help? – Ted Aug 15 '19 at 10:29
0
pd.to_numeric(df, errors='coerce')

needs to be assigned like:

df = pd.to_numeric(df, errors='coerce')

All strings in the columns should be replaced with "NaN"s now, thus making df.groupby("A", as_index=False).mean() possible.

cptnJ
  • 230
  • 2
  • 8