【发布时间】:2025-12-15 20:35:01
【问题描述】:
我想改造下面的字典
x = {
'John': ['1.12.2021','2.12.2021','3.02.2022','4.2.2022','5.07.2022','6.07.2022','7.12.2022','8.12.2022'],
'Andrew': ['1.12.2021','2.03.2022','3.03.2022','4.05.2022','5.05.2022','6.09.2022','7.09.2022','8.11.2022','9.12.2022','10.12.2022']}
到这样的 DataFrame 中,其中的列将显示每月计数
Name 12.2021 02.2022 03.2022 05.2022 07.2022 09.2022 11.2022 \
John 2 2 0 0 2 0 0
Andrew 1 0 2 2 0 2 1
12.2022
2
2
我从这个开始,将值转换为日期时间:
x = pd.DataFrame.from_dict(x, 'index').reset_index().fillna(value='0')
x.iloc[:,1:] = pd.to_datetime(x.iloc[:,1:], format='%d.%m.%Y')
print(x)
但是得到这个错误:AttributeError: 'int' object has no attribute 'lower'
【问题讨论】:
-
我真的是第一天,只有十二月的日期,我没有得到输出
-
@mozway 感谢您的评论,粘贴错误的字典,现在修复它。
-
好的,然后我更新了我的答案;)
标签: python pandas dataframe datetime