我有一个类似
的列表l = [[Alex,Jan,Fri],[John,Feb,Mon],[Alex,Jan,Fri],[Alex,Feb,Mon],[John,Jan,Mon]]
我想过滤出特定月份的列表,例如说“ Jan”,列表应该像这样
l=[[Alex,2],[John,1]]
其中2和1是它们在特定月份中使用熊猫出现在列表中的数量 这就是我尝试过的
import pandas as pd
li = pd.DataFrame(l, columns=['name', 'month', 'day'])
l = li.filter('month'=Jan).name.count().reset_index().values.tolist()
答案 0 :(得分:2)
用于列表理解为Counter
的python解决方案:
from collections import Counter
L = [list(x) for x in Counter([a for a,b,c in l if b == 'Jan']).items()]
print (L)
[['Alex', 2], ['John', 1]]
使用DataFrame.query
和Series.value_counts
的熊猫解决方案:
l = li.query("month=='Jan'").name.value_counts().reset_index().values.tolist()
print (l)
[['Alex', 2], ['John', 1]]
答案 1 :(得分:1)
这可以通过列表理解和字典来完成
l = [['Alex','Jan','Fri'],['John','Feb','Mon'],['Alex','Jan','Fri'],['Alex','Feb','Mon'],['John','Jan','Mon']]
target_month = 'Jan'
result_dict = {}
for obs in l:
if obs[1] == target_month:
if obs[0] in result_dict.keys():
result_dict[obs[0]] += 1
else:
result_dict[obs[0]] = 1
ret_list = []
for k,v in result_dict.items():
ret_list.append([k,v])
print(ret_list)
这将输出:
[['Alex', 2], ['John', 1]]