亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法從字典中打印月份值

無法從字典中打印月份值

陪伴而非守候 2023-01-04 16:17:22
我正在嘗試從這本字典中打印月份名稱,該字典在代碼之前定義得很好:month_index = { '1': 'January',                '2': 'February',                '3': 'March',                '4': 'April',                '5': 'May',                '6': 'June'}我嘗試使用dataframe.mode()某些輸出多列的數據框訪問元素,其中一列生成月份列,其中值僅為 1、2、3、4、5 或 6,代表一月到六月。dataframe.mode正在生產0    66 = 六月。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 中的值?任何幫助將不勝感激。
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

你現在這樣做的方式是檢查 的字典鍵common_month,但你需要檢查值。另外我假設你只想打印最常見的月份,而不是整個字典。只需將 if 語句更改為此即可。

if common_month in month_index.values(): 
           print(f'The most common month is {common_month}.')


查看完整回答
反對 回復 2023-01-04
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

mode() 函數將returning object pandas.core.series.Series數據類型。而且您沒有檢查字典值。所以你必須在 common_month 中使用 any/all,獲取索引,并檢查 dict 中的值。

if common_month.any() in month_index.values(): 
   print('The most common month is', common_month[0], '.')


查看完整回答
反對 回復 2023-01-04
  • 2 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號