【问题标题】:How to change the class of a coulmn in dataframe?如何更改数据框中列的类?
【发布时间】:2018-05-01 09:22:55
【问题描述】:

我将邮政编码作为数据框中的一列,格式如下 19678.0 我想把它转换成 int64 19678 当我尝试运行以下代码时,

call_df['zip']=int(call_df['zip'])

它说 TypeError:无法将系列转换为

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python dataframe type-conversion


    【解决方案1】:

    相信你在找astype()

    call_df['zip'] = call_df['zip'].astype(int)
    

    或者,如果您想专门转换为 int64,请使用:

    call_df['zip'] = call_df['zip'].astype('int64')
    

    【讨论】:

    • 我已经尝试了上述方法,它将列转换为 int 但仍然显示: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from DataFrame。尝试改用 .loc[row_indexer,col_indexer] = value
    • 该警告与类型转换问题无关。很可能您在创建 call_df 时分配了一些其他 DataFrame 的切片。您可以通过将.copy() 链接到您最初用于创建call_df 的任何语句的末尾来消除警告,但最好在此处更详细地阅读此警告:stackoverflow.com/questions/20625582/…跨度>
    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多