我想用这种方式显示,月份转换为datetime,并使用加拿大人来浮点数或整数
我使用了这段代码,但我不断收到错误“几个月没有定义,雇佣了加拿大人”
nfl2 = nfl.melt(id_vars=["Month", "Employer Persons"],
var_name="Province",
value_name="Newfoundland and Labrador")
nfl2 = nfl2.rename(columns={'Province': 'Months','Newfoundland and Labrador': 'Employed Canadians'})
nfl_final['Months'] = nfl_final['Months'].dt.strftime('%y-%m' )
nfl_final = nfl_final['Employer Persons'] = pd.Series(dtype='int')
这应该行得通
请注意,由于没有为每个日期提供年份或日期,datetime对象默认为1970-01-01,因此我最后使用lambda函数将其更改为2020。您可以轻松地将这些日期时间格式化为仅显示为月份名称,但这会将数据类型更改为字符串
你错误地使用了
melt
。我建议您查看文档/教程,了解其工作原理结果:
我允许自己用两个月的时间创建一个小例子。你可以随心所欲地扩展它。请提供代码,以便下次提问时预制作数据帧
当使用
melt
时,您应该将月份声明为“value_vars”(因为它们是值)。索引移位是为了从1开始索引,而不是从0开始索引你不太清楚你到底想要哪种日期时间格式,所以我只花了几个月的时间
%B
将“一月”转换为日期格式,然后我只提取月份相关问题 更多 >
编程相关推荐