【发布时间】:2020-09-29 09:47:20
【问题描述】:
我正在尝试从该字典中打印月份名称,该字典在代码之前定义:
month_index = { '1': 'January',
'2': 'February',
'3': 'March',
'4': 'April',
'5': 'May',
'6': 'June'}
我尝试使用 dataframe.mode() 从某个输出多个列的数据框中访问元素,其中一个列生成一个月列,其中值仅为 1、2、3、4、5 或 6,代表 1 月至 6 月。 dataframe.mode 正在生成 0 6,其中 6 = 六月。
common_month = df['month'].mode()
if common_month in month_index:
print('The most common month is ', month_index, '.')
我应该使用.mode() 的结果创建一个字典,还是只使用df['month'].mode().str[1] 然后使用in 来访问month_index 中的值?任何帮助将不胜感激。
【问题讨论】:
标签: python arrays dataframe dictionary mode