我正在使用的数据集包括要从字符串转换为日期时间格式的列名。我要转换的列名称是columnns 3及以后,并且列的实际值应保留为字符串。
这是我数据框的前2行:
RegionID RegionName SizeRank 1996-04 1996-05 \
1 394913 New York, NY 1 164400.0 164400.0
2 753899 Los Angeles-Long Beach-Anaheim, CA 2 170300.0 170100.0
1996-06 1996-07 1996-08 1996-09 1996-10 ... 2018-07 \
1 164300.0 164100.0 164000.0 163900.0 163800.0 ... 428900
2 169900.0 169700.0 169600.0 169400.0 169300.0 ... 645400
2018-08 2018-09 2018-10 2018-11 2018-12 2019-01 2019-02 2019-03 \
1 429900 431600 434000 436300 437800 439300 441200 442500
2 647500 649400 651100 652400 653600 654100 653400 651600
2019-04
1 442500
2 649500
[2 rows x 280 columns]
这是我的代码:
import pandas as pd
zillow = pd.read_csv('Metro_Zhvi_AllHomes.csv', encoding="Latin-1")
zillow.columns.iloc[3:] = pd.to_datetime(zillow, '%Y-%m')
这是错误消息:
/anaconda3/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _assemble_from_unit_mappings(arg, errors)
540 raise ValueError("to assemble mappings requires at least that "
541 "[year, month, day] be specified: [{required}] "
--> 542 "is missing".format(required=','.join(req)))
543
544 # keys we don't recognize
ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing