合并两个大熊猫dataframes与时间序列指数
问题描述:
我有两只大熊猫dataframes,我想合并/联合起来合并两个大熊猫dataframes与时间序列指数
例如:
#required packages
import os
import pandas as pd
import numpy as np
import datetime as dt
# create sample time series
dates1 = pd.date_range('1/1/2000', periods=4, freq='10min')
dates2 = dates1
column_names = ['A','B','C']
df1 = pd.DataFrame(np.random.randn(4, 3), index=dates1,
columns=column_names)
df2 = pd.DataFrame(np.random.randn(4, 3), index=dates2,
columns=column_names)
df3 = df1.merge(df2, how='outer', left_index=True, right_index=True, suffixes=('_x', '_y'))
在这里,我想在这两个数据集合并下面的方式(注列的顺序):
A_x A_y B_x B_y C_x C_y
2000-01-01 00:00:00 2000-01-01 00:00:00 -0.572616 -0.867554 -0.382594 1.866238 -0.756318 0.564087
2000-01-01 00:10:00 2000-01-01 00:10:00 -0.814776 -0.458378 1.011491 0.196498 -0.523433 -0.296989
2000-01-01 00:20:00 2000-01-01 00:20:00 -0.617766 0.081141 1.405145 -1.183592 0.400720 -0.872507
2000-01-01 00:30:00 2000-01-01 00:30:00 1.083721 0.137422 -1.013840 -1.610531 -1.258841 0.142301
我想通过创建一个多索引数据帧或创建用于第二索引的列以保存两个数据帧索引。使用merge_ordered而不是merge或join会更容易吗?
任何帮助表示赞赏。
答
使用CONCAT
pd.concat([df1.reset_index().add_suffix('_x'),\
df2.reset_index().add_suffix('_y')], axis = 1)\
.set_index(['index_x', 'index_y'])
A_x B_x C_x A_y B_y C_y
index_x index_y
2000-01-01 00:00:00 2000-01-01 00:00:00 -1.437311 -1.414127 0.344057 -0.533669 -0.260106 -1.316879
2000-01-01 00:10:00 2000-01-01 00:10:00 0.662025 1.860933 -0.485169 -0.825603 -0.973267 -0.760737
2000-01-01 00:20:00 2000-01-01 00:20:00 -0.300213 0.047812 -2.279631 -0.739694 -1.872261 2.281126
2000-01-01 00:30:00 2000-01-01 00:30:00 1.499468 0.633967 -1.067881 0.174793 1.197813 -0.879132
答
我想你想concat
,而不是合并:
In [11]: pd.concat([df1, df2], keys=["df1", "df2"], axis=1)
Out[11]:
df1 df2
A B C A B C
2000-01-01 00:00:00 1.621737 0.093015 -0.698715 0.319212 1.021829 1.707847
2000-01-01 00:10:00 0.780523 -1.169127 -1.097695 -0.444000 0.170283 1.652005
2000-01-01 00:20:00 1.560046 -0.196604 -1.260149 0.725005 -1.290074 0.606269
2000-01-01 00:30:00 -1.074419 -2.488055 -0.548531 -1.046327 0.895894 0.423743
答
merge
确实将合并这两个指数。
在合并之前,您可以创建df2
额外列:
df2["index_2"]=df2.index
这将创造的最终结果列,这将是该指数在df2
值。
请注意,唯一的情况下这个列将不同于索引是当元素没有出现在df2
,在这种情况下,它将为空,所以我不知道我理解你的最终目标。
我认为这是接近我想要的,但列的顺序并不是我想要的。我需要订购A_x A_y B_x B_y ...等等。 有什么办法来排序列的方式?也许这是一个排序功能? –
@MooseDrool,你可以使用df.sort_index(axis = 1,inplace = True) – Vaishali
这很有道理! –