在Python中更改数据框的单列或多列的数据类型

问题描述

我有一个数据框,我想更改不同列的数据类型。我在每个列中都有不同的数据类型,因此已制定了数据范围的架构。

import pandas as pd
import numpy as np
orders_schema = {
  '?dummy'              :np.int64,'@timestamp'          :np.str,'@version'            :np.int64,'bytes'               :np.int64,'c-geoip'             :np.str,'c-isp'               :np.str,'client'              :np.str,'duration_usec'       :np.int64,'forwarded_for'       :np.str,'gtmcb'               :np.float64,'handling'            :np.str,'hit_miss'            :np.str,'host'                :np.str,'method_urlv'         :np.str,'ns__t'               :np.float64,'ns_jspageurl'      :np.str,'ns_referrer'         :np.str,'parsed-useragent'    :np.str,'port'                :np.int64,'referer'             :np.str,'remote_user'         :np.str,'request'             :np.str,'nav'                 :np.str,'pp'                  :np.str,'profileid'           :np.float64,'source'              :np.str,'titel'               :np.str,'type'                :np.str,'userid'              :np.str,'uuid'                :np.str,'status'              :np.int64,'tags'                :np.str,'time_first_byte'     :np.float64,'topic'               :np.str,'user-agent'          :np.str,'version'             :np.str,'lhost'               :np.str
}

但是,我不知道如何将该模式应用于现有数据框。我尝试过

df_2.astype(orders_schema)
KeyError: 'Only a column name can be used for the key in a dtype mappings argument.'

您能否建议我如何更改列的数据类型?

预先感谢

解决方法

您只能在order_schema的键中提供列名。所有这些列名都是