Austin
Austin

Reputation: 7329

Pandas list comprehension over columns instead of rows

I'm trying to convert the following loop to list comprehension:

# convert integer-only valued columns from type float to type int
for col in df:
    if sum(df[col]%1) == 0:
        df[col]=df[col].astype(int)

I can't seem to get the syntax for this to work properly. This is what I've tried so far:

new_df = pd.DataFrame([df[col].astype(int) if sum(df[col]%1)==0 else df[col] for col in df])

This seems to transpose my data frame and not actually convert the types. Would someone please help me out? Also if there's a more idiomatic way to convert int-only valued columns from float to int type I'm open to a different approach.

Upvotes: 1

Views: 1420

Answers (2)

jezrael
jezrael

Reputation: 862691

You can use concat:

df = pd.concat([df[col].astype(int) if sum(df[col]%1)==0 else df[col] for col in df], 1)

Or:

df = df.astype(df.dtypes.mask((df%1==0).all(), 'int'))

Upvotes: 3

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

Why can't we use np.where and a dict to map dtypes i.e

df = pd.DataFrame({'co1':[1.,2.,3.,4.],'co2':[2.5,6.5,7.5,1.3]})

df = df.astype(dict(zip(df.columns,np.where((df%1==0).all(),np.int,df.dtypes))))

   co1  co2
0  1   2.5
1  2   6.5
2  3   7.5
3  4   1.3

Upvotes: 2

Related Questions