import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Jill', 'John', 'Bob'],
'Sport' : ['Golf', 'Rugby', 'Hockey', 'Golf'],
'Score': [100, 50, 100, 45]})
# the names of people that played Golf
df[df['Sport'] == 'Golf']['Name'].unique()
>> ['John' 'Bob']
# all of the people that scored 100 on any sport.
df[df['Score'] == 100]['Name'].unique()
>> ['John']
# all of the data for just John.
df[df['Name'] == 'John']
>> Name Score Sport
0 John 100 Golf
2 John 100 Hockey
from collections import namedtuple
# Create a named tuple to store the rows
Row = namedtuple('Row', ('name', 'sport', 'score'))
data = '''Name Sport Score
John Golf 100
Jill Rugby 55
John Hockey 100
Bob Golf 45'''
# Read the data, skip the first line
lines = data.splitlines()[1:]
rows = []
for line in lines:
name, sport, score = line.strip().split()
rows.append(Row(name, sport, int(score)))
# People that played Golf
golf_filter = lambda row: row.sport == 'Golf'
golf_players = filter(golf_filter, rows)
# People that scored 100 on any sport
score_filter = lambda row: row.score == 100
scorers = filter(score_filter, rows)
# People named John
john_filter = lambda row: row.name == 'John'
john_data = filter(john_filter, rows)
# If you want a specific column than you can map the data
# Names of golf players
get_name = lambda row: row.name
golf_players_names = map(get_name, golf_players)
pandas
'DataFrame
将是一条道路:这个怎么样?你知道吗
这应该保持列表排序时每个条目的关系。你知道吗
为了避免列表中重复元素的影响,首先从列表中创建一个新的
set
。你知道吗对于预期的查询,可以这样做。你知道吗
最好使用
list
来存储结果,并使其成为set
,以避免某些情况,例如,如果一个人在两个不同的游戏中得分为x
。你知道吗带有
namedtuple
和lambda
的map
和filter
可用于此任务。你知道吗结果:
相关问题 更多 >
编程相关推荐