Home > Back-end >  How to change column dtypes via list comprehension?
How to change column dtypes via list comprehension?

Time:05-10

How can I use a list comprehension to set the dtype of the pandas columns which are currently set as 'object' (i.e. the first two columns alpha and hl) ? I tried the below list compreshension but it generated a syntax error.

import pandas as pd

alpha = {'alpha': ['0.0001', '0.001', '0.01', '0.1']}
hl = {'hl': [(16, ), (16,16), (64, 64,), (128, 128)]}
score = {'score': [0.65, 0.75, 0.85, 0.95]}

data = {}

for i in [alpha, hl, score]:
    data.update(i)

df = pd.DataFrame(data)

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   alpha   4 non-null      object
 1   hl      4 non-null      object
 2   score   4 non-null      float64


[df[c].astype('string') if df[c].dtype == 'O' for c in df.columns]

I get the following error:

[df[c].astype('string') if df[c].dtype == 'O' for c in df.columns]
                                                  ^
SyntaxError: invalid syntax

CodePudding user response:

I am not sure if it is what you are looking for and why would you want to do it with a list comprehension is not quite clear to me. However, this seems to fix the error.

import pandas as pd

[df[col].astype({col : str}) for col in df.columns if df[col].dtype == 'object']
  • Related