逐行数据帧分割
问题描述:
鉴于以下数据框:逐行数据帧分割
df = pd.DataFrame(data={'item': [1, 2, 3, 4], 'start':[0.0, 2.0, 8.0, 6.0],
'end': [2.0, 6.0, 8.0, 14.0]})
如何快速通过分段间隔“开始”扩大上述数据帧逐行 - “端”为2的倍数?
对于上面的例子,所产生的数据帧应该是
Out=
item start end
1 0.0 2.0
2 2.0 4.0
2 4.0 6.0
3 8.0 8.0
4 6.0 8.0
4 8.0 10.0
4 10.0 12.0
4 12.0 14.0
性能是最重要的对我来说,因为我有几百万行的检查。 我已经使用布尔索引为那些不需要分段的行过滤整个数据帧。这是一个很快的速度然而,在其余的行中,我应用了一个'for循环',并且创建了正确长度的数据框,我一直在追加。不幸的是,数百万行的性能不足。
期待专家的解决方案!
答
开始从原来的数据框中:
import pandas as pd
import numpy as np
df = pd.DataFrame(data={'item': [1, 2, 3, 4], 'start':[0.0, 2.0, 8.0, 6.0],
'end': [2.0, 6.0, 10.0, 14.0]})
然后,运行下面的代码:
lengths = pd.Series([1, 2, 1, 4]) # For the example, I just created this array,
# but obviously I would use the mod function to
# determine the number of segments to create
# Row below elongates the dataframe according to the array 'lengths'
df = df.reindex(np.repeat(df.index.values, lengths), method='ffill')
df['start'] += pd.Series(df.groupby(level=0).cumcount()*2.0)
df['end'] = df['start'] + 2.0
print df
注意初始数据帧包含一个错误。项目'3'需要'start = 8.0'和'end = 10.0'。
由于使用了熊猫Cython函数,我相信这种方法非常快捷。当然,还有其他的可能性。
答
您可以编写一个函数,该函数返回展开的开始和结束时间的DataFrame。在这个例子中,我group,因为我不知道你可以返回一个DataFrame从apply
没有它首先分组。
def convert(row):
start = row.start.values[0]
end = row.end.values[0]
if start == end:
return pd.DataFrame([[start, end]], columns=['start', 'end'])
else:
return pd.DataFrame({'start': np.arange(start, end, 2),
'end':np.arange(start + 2, end + 2, 2)},
columns=['start', 'end'])
df1=df.groupby('item').apply(convert)
df1.index = df1.index.droplevel(1)
df1.reset_index()
item start end
0 1 0.0 2.0
1 2 2.0 4.0
2 2 4.0 6.0
3 3 8.0 8.0
4 4 6.0 8.0
5 4 8.0 10.0
6 4 10.0 12.0
7 4 12.0 14.0
谢谢。我之前做过类似的事情。我发布了一个我认为更快的替代解决方案。 –