Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中更改Dataframe的单个或多个列的数据类型_Python_Pandas_Data Wrangling - Fatal编程技术网

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

在Python中更改Dataframe的单个或多个列的数据类型,python,pandas,data-wrangling,Python,Pandas,Data Wrangling,我有一个数据框,我想更改不同列的数据类型。我已经为数据范围建立了一个模式,因为我在每列中都有不同的数据类型 import pandas as pd import numpy as np orders_schema = { '?dummy' :np.int64, '@timestamp' :np.str, '@version' :np.int64, 'bytes'

我有一个数据框,我想更改不同列的数据类型。我已经为数据范围建立了一个模式,因为我在每列中都有不同的数据类型

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,    
  'type'                :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.'
您能建议我如何更改列的数据类型吗


提前感谢

您只能在顺序键模式中提供列名。所有这些列名都是

您只能在顺序键模式中提供列名。所有这些列名都是列名吗?

事实上,它们都是列名,需要这些列的数据类型。我认为列名中的任何一个都与词典中提供的不匹配,因为这就是错误所在。如果可能的话,你可以分享ss吗?事实上,它们都是列名,并且需要这些列的数据类型。我认为任何一个列名都与你在字典中提供的不匹配,因为这就是错误所在。请您提供ss或重新检查列名好吗?请仔细检查列名没有问题。如果可能,您可以共享ss吗?请共享样本数据