我正在嘗試使用matplotlib創建元素頻率的條形圖。 爲了做到這一點,我需要能夠統計一個pandas數據框列中出現的標誌列表數量。 下面會給代碼的草圖我已經在我的筆記本/數據:計算熊貓數據框中出現的列表
# list of filtered values
filtered = [200, 201, 201, 201, 201, 201,
211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211,
237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237,
237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237, 237,
237, 237, 237, 237, 237, 237, 237, 237, 250, 250, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250,
250, 250, 250, 250, 254]
# list of flags to use for filtering
flags = [200, 201, 211, 237, 239, 250, 254, 255]
# this was just a line to code for testing
flags_dict = {200:0,201:0,211:0,237:0,239:0,250:0,254:0,255:0}
freq = filtered.value_counts()
"""
Expected flags_dict:
200: 1
201: 5
211: 14
237: 38
239: 0
250: 40
254: 1
255: 0
"""
"""
These are the values from the real dataframe but they do not take into
account the other flags in the flags list
freq:
250.0 7682
211.0 3734
200.0 1483
239.0 180
201.0 34
"""