我有一个数据帧df如下:
Name Race(m) Date
Peter 2000 23/09/16
Mary 100 23/09/16
Mary 400 23/09/16
Mary 200 23/09/16
Mary 400 24/09/17
John 800 23/09/16
我想添加一个专栏[X]来表示该人在同一天仍有多少次比赛:
Name Race(m) Date X
Peter 2000 23/09/16 1
Peter 100 23/09/16 0
Mary 400 23/09/16 1
Mary 200 23/09/16 0
Mary 400 24/09/17 0
John 800 23/09/16 0
实现目标:
content = df.values.tolist()
name = []
date = []
for each in content:
if (each[0] not in name and (each[2] not in date)) or ((each[0] in name) and (each[2] not in date)) or ((each[0] not in name) and (each[2] in date)):
remaining_race = 0
for every in content:
if every[0] == each[0] and every[2] == each[2]:
remaining_race += 1
remaining_race -= 1
name.append(each[0])
date.append(each[2])
each.append(remaining_race)
else:
remaining_race -= 1
each.append(remaining_race)
然而,输出是:
Name Race(m) Date X
Peter 2000 23/09/16 1
Peter 100 23/09/16 0
Mary 400 23/09/16 -1
Mary 200 23/09/16 -2
Mary 400 24/09/17 0
John 800 23/09/16 -3
我该如何解决?
答案 0 :(得分:0)
import pandas as pd
df = pd.DataFrame(data=[["peter"],["peter"]], columns=["name"])
# count number of times each name occurs, put it in a dictionary
names = dict(df["name"].value_counts())
def num_races_left(name):
names[name]-=1
return names[name]
# for every name, look it up in the dict and reduce number by 1
df["X"] = df["name"].apply(num_races_left)
返回以下内容
name X
0 peter 1
1 peter 0