如何更改最初属于类别的pandas Data Frame列的类型?

时间:2015-04-24 10:14:17

标签: python python-2.7 pandas

我的数据框df中有以下列:

df.dtypes
MeanTemperatureC              int64
MaxTemperatureC             float64
MinTemperatureC               int64
DewPointC                     int64
MeanDewPointC                 int64
MinDewpointC                  int64
MaxHumidity                   int64
MeanHumidity                  int64
MinHumidity                   int64
MaxSeaLevelPressurehPa        int64
MeanSeaLevelPressurehPa     float64
MinSeaLevelPressurehPa        int64
MaxVisibilityKm               int64
MeanVisibilityKm            float64
MinVisibilitykM             float64
MaxWindSpeedKm/h              int64
MeanWindSpeedKm/h             int64
MeanTemperatureCT          category
dtype: object

我想将列'MeanTemperatureCT'的数据类型更改为str。 我已经尝试了以下内容:

df['MeanTemperatureCT'].astype(np.str)

并没有改变数据类型。所以,请帮忙。

1 个答案:

答案 0 :(得分:5)

df['MeanTemperatureCT']= df['MeanTemperatureCT'].astype(str)