Python 熊猫:用特定索引匹配减去两个序列
我有一个巨大的数据集,我需要处理大量的数字,然后寻找1。一个实际的解决方案和2。快一点 我试图尽可能地简化我的问题,并将其转化为一个真实的例子,我希望这是清楚的。我很确定(至少我希望如此)这在熊猫身上是一个普遍的问题,而不是一个非常特殊的问题 假设我在一家名为Python 熊猫:用特定索引匹配减去两个序列,python,pandas,indexing,Python,Pandas,Indexing,我有一个巨大的数据集,我需要处理大量的数字,然后寻找1。一个实际的解决方案和2。快一点 我试图尽可能地简化我的问题,并将其转化为一个真实的例子,我希望这是清楚的。我很确定(至少我希望如此)这在熊猫身上是一个普遍的问题,而不是一个非常特殊的问题 假设我在一家名为foo和bar的公司有两名员工。有时他们在同一天工作,一遍又一遍地做同样的任务。我测量他们完成任务所需的时间(有时一天一次,有时多次) 我现在要寻找的是,如果两人在同一天工作,最短时间之间的差异 我尝试使用整体方法,因此尽可能多地处理表(而
foo
和bar
的公司有两名员工。有时他们在同一天工作,一遍又一遍地做同样的任务。我测量他们完成任务所需的时间(有时一天一次,有时多次)
我现在要寻找的是,如果两人在同一天工作,最短时间之间的差异
我尝试使用整体方法,因此尽可能多地处理表(而不是迭代)
我目前的策略是:按天
和名称
对数据进行分组,如果分组大小为2(这意味着我在同一天有两个工作人员的数据),则只保留每个天和的最短时间
,减去两次
最终目标:拥有一系列最短时间的差异
但是,我没有进行分组和筛选,所以现在我尝试为两个工作人员创建两个序列,然后计算时间差
以下是一个示例数据集:
from StringIO import StringIO
import pandas as pd
raw_data="""day name time
1 foo 10
1 foo 9
1 bar 4
2 foo 12
2 foo 13
3 bar 3
3 bar 5
5 foo 8
5 bar 5
5 foo 9
5 bar 1
"""
df = pd.read_csv(StringIO(raw_data), sep=' ')
grouped_by_day_and_name = df.groupby(['day', 'name'])
这是分组并仅保留最短时间后的表的外观:
print grouped_by_day_and_name.agg({'time': min})
time
day name
1 bar 4
foo 9
2 foo 12
3 bar 3
5 bar 1
foo 8
现在我只对第1天和第5天感兴趣,因为只有这两天我有bar
和foo
的数据。因此,如果我能以某种方式过滤数据并在每组中减去两次,那么结果将是[-5,-7]
(从第1天:4-9,第5天1-8)
由于我无法进行过滤和减法,因此我尝试为两个名称创建一个系列,并对每个名称进行减法,但是索引不匹配:
foo_best_times = df[df.name == 'foo'].groupby(['day', 'name']).agg({'time': min})
bar_best_times = df[df.name == 'bar'].groupby(['day', 'name']).agg({'time': min})
尝试减去每一项后:
print foo_best_times - bar_best_times
time
day name
1 bar NaN
foo NaN
2 foo NaN
3 bar NaN
5 bar NaN
foo NaN
我的目标是这样的:
day time
1 -5
2 NaN
3 NaN
5 -7
如何通过只匹配日
作为索引来减去这两个序列
这是快速完成任务的正确方法吗?我认为您想要做的是“内部”连接。此类型的联接将执行您要查找的索引匹配:
from StringIO import StringIO
import pandas as pd
raw_data="""day name time
1 foo 10
1 foo 9
1 bar 4
2 foo 12
2 foo 13
3 bar 3
3 bar 5
5 foo 8
5 bar 5
5 foo 9
5 bar 1
"""
df = pd.read_csv(StringIO(raw_data), sep=' ')
# Split the dataset into the two workers
foo = df.query('name == "foo"')
bar = df.query('name == "bar"')
# Find for each day the shortest working time
foo = foo.groupby('day').agg('min')
bar = bar.groupby('day').agg('min')
# Perform an inner join of the two workers, this only keeps days
# where both workers have been working
joined = foo.join(bar, how='inner', lsuffix='_foo', rsuffix='_bar')
# Compute the difference in minimum working times
diff = joined['time_bar'] - joined['time_foo']
print diff
结果:
day
1 -5
5 -7
dtype: int64
day
1 -5
2 NaN
3 NaN
5 -7
dtype: float64
如果您希望在只有一名工作人员工作的日子使用NaN,您可以执行“外部”联接:
# Perform an outer join of the two workers, this only keeps days
# where both workers have been working
joined = foo.join(bar, how='outer', lsuffix='_foo', rsuffix='_bar')
# Compute the difference in minimum working times
diff = joined['time_bar'] - joined['time_foo']
print diff
结果:
day
1 -5
5 -7
dtype: int64
day
1 -5
2 NaN
3 NaN
5 -7
dtype: float64
我认为您可以使用withaggfunc=min
,然后减去列bar
和foo
:
from StringIO import StringIO
import pandas as pd
raw_data="""day name time
1 foo 10
1 foo 9
1 bar 4
2 foo 12
2 foo 13
3 bar 3
3 bar 5
5 foo 8
5 bar 5
5 foo 9
5 bar 1
"""
df = pd.read_csv(StringIO(raw_data), sep=' ')
print df
day name time
0 1 foo 10
1 1 foo 9
2 1 bar 4
3 2 foo 12
4 2 foo 13
5 3 bar 3
6 3 bar 5
7 5 foo 8
8 5 bar 5
9 5 foo 9
10 5 bar 1
在你的类比中,可能只有两名员工吗?也许你可以重置索引,比如打印foo\u最佳时间。重置索引(级别=1)['time']-bar\u最佳时间。重置索引(级别=1)['time']
基本上也可以有多名员工,但我试图自己解决这个问题。如果我把两个工人的问题解决了,那就太大了;-)这是非常快的!而且它似乎还与多个“员工”合作。