[英]python pandas parse_dates for multiple columns in csv file
I'm working on Python pandas for two csv files comparison but in csv files having 5 date columns i've used parse_date=['dateofbirth','lastupdates','dateofjoin','dateofresign,'endoftrade'] in read_csv method but it is only parsing dateofbirth not all the columns in csv file.我正在使用 Python pandas 进行两个 csv 文件的比较,但在具有 5 个日期列的 csv 文件中,我在 read_csv 方法中使用了 parse_date=['dateofbirth','lastupdates','dateofjoin','dateofresign,'endoftrade'] 但是它只解析出生日期而不是 csv 文件中的所有列。
code:代码:
csv_pandas=pd.read_csv("path of the csv file",parse_date=['dateofbirth','lastupdates','dateofjoin','dateofresign,'endoftrade'])
print(csv_pandas)
CSV File: CSV 文件:
dateofbirth lastupdates dateofjoin dateofresign
05/06/2021 00:00:00PM 12/13/2021 12:00:00PM 12/13/2021 12:00:00PM 12/13/2021 12:00:00PM
column non-null count Dtype
------ ------------- ------
dateofbirth non-null object
dateofbirth non-null datetime64[ns]
dateofbirth non-null datetime64[ns]
dateofbirth non-null datetime64[ns]
I can able to convert only object Dtype column,remaining datetime64[ns] not parsing我只能转换对象 Dtype 列,剩余的 datetime64[ns] 不解析
Around i've 160 csv files ,each csv file have different column names ,Can any one plz suggest我大约有 160 个 csv 文件,每个 csv 文件都有不同的列名,任何人都可以建议
strptime()
format instructions.您有两种日期格式需要不同的strptime()
格式指令。assign()
并非您尝试转换的所有列都存在于数据框中,因此测试该列存在于作为 ** kwargs传递assign()
dict理解中csv_pandas = csv_pandas.assign(
**{
c: pd.to_datetime(csv_pandas[c], format="%Y-%m-%d %H:%M:%S:%f", errors="ignore")
for c in parse_date
if c in csv_pandas.select_dtypes("object").columns
}
).pipe(
lambda d: d.assign(
**{
c: pd.to_datetime(d[c], format="%m/%d/%Y %H:%M:%S%p", errors="ignore")
for c in parse_date
if c in d.select_dtypes("object").columns
}
)
)
csv_pandas.dtypes
dateofbirth datetime64[ns]
lastupdates datetime64[ns]
dateofjoin datetime64[ns]
dateofresign datetime64[ns]
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.