![](/img/trans.png)
[英]compare column values of dataframes with non-unique indices of different length
[英]Merge multiple dataframes with non-unique indices
我有一堆熊貓時間序列。 下面是一個示例(實際數據在每個系列中有大約100萬個條目):
>>> for s in series:
print s.head()
print
2014-01-01 01:00:00 -0.546404
2014-01-01 01:00:00 -0.791217
2014-01-01 01:00:01 0.117944
2014-01-01 01:00:01 -1.033161
2014-01-01 01:00:02 0.013415
2014-01-01 01:00:02 0.368853
2014-01-01 01:00:02 0.380515
2014-01-01 01:00:02 0.976505
2014-01-01 01:00:02 0.881654
dtype: float64
2014-01-01 01:00:00 -0.111314
2014-01-01 01:00:01 0.792093
2014-01-01 01:00:01 -1.367650
2014-01-01 01:00:02 -0.469194
2014-01-01 01:00:02 0.569606
2014-01-01 01:00:02 -1.777805
dtype: float64
2014-01-01 01:00:00 -0.108123
2014-01-01 01:00:00 -1.518526
2014-01-01 01:00:00 -1.395465
2014-01-01 01:00:01 0.045677
2014-01-01 01:00:01 1.614789
2014-01-01 01:00:01 1.141460
2014-01-01 01:00:02 1.365290
dtype: float64
每個系列中的時間並不是唯一的。 例如,最后一個系列在2014-01-01 01:00:00
有3個值。 第二個系列當時只有一個值。 此外,並非所有時間都需要出現在所有系列中 。
我的目標是創建一個合並的DataFrame
,其時間是各個時間序列中所有時間的並集。 每個時間戳應根據需要重復多次。 因此,如果上述系列中的時間戳出現(2, 0, 3, 4)
DataFrame
(2, 0, 3, 4)
次,則時間戳應在結果DataFrame
重復4次(最大頻率)。 每列的值應“向前填充”。
例如,合並上述結果應該是:
c0 c1 c2
2014-01-01 01:00:00 -0.546404 -0.111314 -0.108123
2014-01-01 01:00:00 -0.791217 -0.111314 -1.518526
2014-01-01 01:00:00 -0.791217 -0.111314 -1.395465
2014-01-01 01:00:01 0.117944 0.792093 0.045677
2014-01-01 01:00:01 -1.033161 -1.367650 1.614789
2014-01-01 01:00:01 -1.033161 -1.367650 1.141460
2014-01-01 01:00:02 0.013415 -0.469194 1.365290
2014-01-01 01:00:02 0.368853 0.569606 1.365290
2014-01-01 01:00:02 0.380515 -1.777805 1.365290
2014-01-01 01:00:02 0.976505 -1.777805 1.365290
2014-01-01 01:00:02 0.881654 -1.777805 1.365290
在我的真實數據中給出大小和“唯一性”的概念:
>>> [len(s.index.unique()) for s in series]
[48617, 48635, 48720, 48620]
>>> len(times)
51043
>>> [len(s) for s in series]
[1143409, 1143758, 1233646, 1242864]
這是我嘗試過的:
我可以創建所有獨特時間的聯合:
uniques = [s.index.unique() for s in series]
times = uniques[0].union_many(uniques[1:])
我現在可以使用times
索引每個系列:
series[0].loc[times]
但是,這似乎重復值,每個項目times
,這是不是我想要的。
我不能使用times
reindex()
系列因為每個系列的索引不是唯一的。
我可以通過一個緩慢的Python循環來完成它,或者在Cython中完成它,但是有一種“只有熊貓”的方式來做我想做的事情嗎?
我使用以下代碼創建了我的示例系列:
def make_series(n=3, rep=(0,5)):
times = pandas.date_range('2014/01/01 01:00:00', periods=n, freq='S')
reps = [random.randint(*rep) for _ in xrange(n)]
dates = []
values = numpy.random.randn(numpy.sum(reps))
for date, rep in zip(times, reps):
dates.extend([date]*rep)
return pandas.Series(data=values, index=dates)
series = [make_series() for _ in xrange(3)]
這是非常近CONCAT:
In [11]: s0 = pd.Series([1, 2, 3], name='s0')
In [12]: s1 = pd.Series([1, 4, 5], name='s1')
In [13]: pd.concat([s0, s1], axis=1)
Out[13]:
s0 s1
0 1 1
1 2 4
2 3 5
但是 ,concat無法處理重復的索引(它們應該如何合並它們,並且在你的情況下你不想以“普通”的方式合並它們 - 作為組合)......
我想你打算用groupby:
In [21]: s0 = pd.Series([1, 2, 3], [0, 0, 1], name='s0')
In [22]: s1 = pd.Series([1, 4, 5], [0, 1, 1], name='s1')
注意:我附加了一個更快的方法,適用於類似int的dtypes(如datetime64)。
我們想為每個項目添加一個MultiIndex級別的cumcounts ,這樣我們就可以使Index變得獨特:
In [23]: s0.groupby(level=0).cumcount()
Out[23]:
0 0
0 1
1 0
dtype: int64
注意:我似乎無法在不作為DataFrame的情況下將列附加到索引。
In [24]: df0 = pd.DataFrame(s0).set_index(s0.groupby(level=0).cumcount(), append=True)
In [25]: df1 = pd.DataFrame(s1).set_index(s1.groupby(level=0).cumcount(), append=True)
In [26]: df0
Out[26]:
s0
0 0 1
1 2
1 0 3
現在我們可以繼續這些:
In [27]: res = pd.concat([df0, df1], axis=1)
In [28]: res
Out[28]:
s0 s1
0 0 1 1
1 2 NaN
1 0 3 4
1 NaN 5
如果你想放棄cumcount級別:
In [29]: res.index = res.index.droplevel(1)
In [30]: res
Out[30]:
s0 s1
0 1 1
0 2 NaN
1 3 4
1 NaN 5
現在你可以填寫以獲得所需的結果...(如果你擔心前向填充不同的日期時間,你可以通過索引和ffill組合)。
如果每組中的重復上限是合理的(我選擇1000,但更高的仍然是“合理的”!),你可以使用Float64Index如下(當然看起來更優雅):
s0.index = s0.index + (s0.groupby(level=0)._cumcount_array() / 1000.)
s1.index = s1.index + (s1.groupby(level=0)._cumcount_array() / 1000.)
res = pd.concat([s0, s1], axis=1)
res.index = res.index.values.astype('int64')
注意:我在這里使用私有方法,它將cumcount作為numpy數組返回...
注意2:這是pandas 0.14,在0.13中你必須將一個numpy數組傳遞給_cumcount_array
例如np.arange(len(s0))
),0.13之前你運氣不好 - 沒有cumcount。
怎么樣 - 首先轉換為帶有標記列的數據幀,然后轉換為concat()。
s1 = pd.Series(index=['4/4/14','4/4/14','4/5/14'],
data=[12.2,0.0,12.2])
s2 = pd.Series(index=['4/5/14','4/8/14'],
data=[14.2,3.0])
d1 = pd.DataFrame(a,columns=['a'])
d2 = pd.DataFrame(b,columns=['b'])
final_df = pd.merge(d1, d2, left_index=True, right_index=True, how='outer')
這給了我
a b
4/4/14 12.2 NaN
4/4/14 0.0 NaN
4/5/14 12.2 14.2
4/8/14 NaN 3.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.