如何通过列表理解更改列数据类型?

How to change column dtypes via list comprehension?

如何使用列表理解来设置当前设置为 'object' 的 pandas 列的数据类型(即前两列 alphahl)?我尝试了下面的列表理解,但它产生了语法错误。

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]

我收到以下错误:

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

我不确定这是否是您要查找的内容,我不太清楚为什么要使用列表理解来完成此操作。但是,这似乎修复了错误。

import pandas as pd

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