python - 熊猫将值与前一行与过滤条件进行比较
问题描述
我有一个包含员工工资信息的 DataFrame。大约有 900000+ 行。
样本:
+----+-------------+---------------+----------+
| | table_num | name | salary |
|----+-------------+---------------+----------|
| 0 | 001234 | John Johnson | 1200 |
| 1 | 001234 | John Johnson | 1000 |
| 2 | 001235 | John Johnson | 1000 |
| 3 | 001235 | John Johnson | 1200 |
| 4 | 001235 | John Johnson | 1000 |
| 5 | 001235 | Steve Stevens | 1000 |
| 6 | 001236 | Steve Stevens | 1200 |
| 7 | 001236 | Steve Stevens | 1200 |
| 8 | 001236 | Steve Stevens | 1200 |
+----+-------------+---------------+----------+
数据类型:
table_num: string
name: string
salary: float
我需要添加一个列,其中包含有关增加\减少的工资水平的信息。我正在使用该shift()
函数来比较行中的值。
主要问题是在整个数据集中对所有唯一员工进行过滤和迭代。
我的脚本大约需要 3 个半小时。
如何更快地做到这一点?
我的脚本:
# giving us only unique combination of 'table_num' and 'name'
# since there can be same 'table_num' for different 'name'
# and same names with different 'table_num' appears sometimes
names_df = df[['table_num', 'name']].drop_duplicates()
# then extracting particular name and table_num from Series
for i in range(len(names_df)): ### Bottleneck of whole script ###
t = names_df.iloc[i,[0,1]][0]
n = names_df.iloc[i,[0,1]][1]
# using shift() and lambda to check if there difference between two rows
diff_sal = (df[(df['table_num']==t)
& ((df['name']==n))]['salary'] - df[(df['table_num']==t)
& ((df['name']==n))]['salary'].shift(1)).apply(lambda x: 1 if x>0 else (-1 if x<0 else 0))
df.loc[diff_sal.index, 'inc'] = diff_sal.values
样本输入数据:
df = pd.DataFrame({'table_num': ['001234','001234','001235','001235','001235','001235','001236','001236','001236'],
'name': ['John Johnson','John Johnson','John Johnson','John Johnson','John Johnson', 'Steve Stevens', 'Steve Stevens', 'Steve Stevens', 'Steve Stevens'],
'salary':[1200.,1000.,1000.,1200.,1000.,1000.,1200.,1200.,1200.]})
样本输出:
+----+-------------+---------------+----------+-------+
| | table_num | name | salary | inc |
|----+-------------+---------------+----------+-------|
| 0 | 001234 | John Johnson | 1200 | 0 |
| 1 | 001234 | John Johnson | 1000 | -1 |
| 2 | 001235 | John Johnson | 1000 | 0 |
| 3 | 001235 | John Johnson | 1200 | 1 |
| 4 | 001235 | John Johnson | 1000 | -1 |
| 5 | 001235 | Steve Stevens | 1000 | 0 |
| 6 | 001236 | Steve Stevens | 1200 | 0 |
| 7 | 001236 | Steve Stevens | 1200 | 0 |
| 8 | 001236 | Steve Stevens | 1200 | 0 |
+----+-------------+---------------+----------+-------+
解决方案
推荐阅读
- html - 如何使 80% 宽度的 div 可水平滚动?
- mongodb - 有没有办法在 $filter (聚合)中对字符串数组使用正则表达式
- r - 使用 facet_grid 时,我可以为除 x 和 y 之外的美学设置自由比例(例如大小)吗?
- nativescript - 我们可以从 nativescript angular playgrond 发送短信吗?
- r - 考虑到以前的更改分配新 ID
- animation - 在中间启动 Xamarin.Forms 动画(循环中)
- java - 如何将 Java 类导入 Jython?
- java - Tomcat 7 + log4j-api-2.11.1.jar 中的注释
- reporting-services - 在 SSRS 中创建父子标签列
- json - 找不到 Coproduct 的隐式读取