我有一个pandas数据帧school_df
,如下所示:
school_id date_posted date_completed
0 A 2014-01-01 2014-01-01
1 A 2014-01-01 2014-01-08
2 A 2014-04-29 2014-05-01
3 B 2014-01-01 2014-01-01
4 B 2014-01-20 2014-02-23
每一行代表该学校的一个项目。我想添加两列:对于每个唯一的school_id
,计算在该日期之前发布的项目数量以及在该日期之前完成的项目数量。
下面的代码有效,但我有大约300,000所独特的学校,所以它需要很长时间才能运行。是否有更快的方式来获得我想要的东西?谢谢你的帮助!
import pandas as pd
groups = school_df.groupby("school_id")
blank_df = pd.DataFrame()
for g, df in groups:
df['school_previous_projects'] = df.date_posted.map(lambda x: len(df[df.date_posted < x]))
df['school_previous_completed'] = df.date_posted.map(lambda x: len(df[df.date_completed < x]))
blank_df = pd.concat([blank_df, df])
答案 0 :(得分:1)
试一试。应该比你的for循环和两个地图更快。从您的框架开始
school_id date_posted date_completed
0 A 2014-01-01 2014-01-01
1 A 2014-01-01 2014-01-08
2 A 2014-04-29 2014-05-01
3 B 2014-01-01 2014-01-01
4 B 2014-01-20 2014-02-23
然后一个功能。 getProjectCounts()使用布尔索引和简单的count()
def getProjectCounts(row, df):
filter = (df["school_id"] == row["school_id"]) & (df["date_posted"] < row["date_posted"])
dp_count = df[filter]["date_posted"].count()
filter = (df["school_id"] == row["school_id"]) & (df["date_completed"] < row["date_completed"])
dc_count = df[filter]["date_completed"].count()
return pd.Series([dp_count, dc_count])
然后一个apply()函数逐行
school_df[["school_previous_projects","school_previous_completed"]] = school_df.apply(lambda x : getProjectCounts(x, school_df),axis=1)
school_id date_posted date_completed school_previous_projects \
0 A 2014-01-01 2014-01-01 0
1 A 2014-01-01 2014-01-08 0
2 A 2014-04-29 2014-05-01 2
3 B 2014-01-01 2014-01-01 0
4 B 2014-01-20 2014-02-23 1
school_previous_completed
0 0
1 1
2 2
3 0
4 1
答案 1 :(得分:0)
这是一个使用cumcount的版本(我简化了日期,但仍然可以使用):
import pandas as pd
import io
df = pd.DataFrame({'school_id': ['A', 'A', 'A', 'B', 'B'],
'date_posted': pd.date_range('2014-01-01', '2014-01-05'),
'date_completed': pd.date_range('2014-01-01', '2014-01-05')})
posted = df.set_index('date_posted').groupby('school_id').cumcount()
comp = df.set_index('date_completed').groupby('school_id').cumcount()
df['posted'] = posted.values
df['comp'] = comp.values
print df
结果:
date_completed date_posted school_id posted comp
0 2014-01-01 2014-01-01 A 0 0
1 2014-01-02 2014-01-02 A 1 1
2 2014-01-03 2014-01-03 A 2 2
3 2014-01-04 2014-01-04 B 0 0
4 2014-01-05 2014-01-05 B 1 1