我有一个pandas DataFrame df: 我想找到或编写一个函数来返回我将在MySQL中使用以下命令返回的DataFrame: 为了以下结果: 通过迭代行并添加到字典,我可以想到通过讨厌的方式来实现这一点,但必须有更好的方法。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有一个任意名单pandas.DataFrame
的(让我们用2保持示例清楚),我想concat
他们的Index
是:
DataFrames
的inner
也不是outer
连接 Index
,但只有所有DataFrame
的日期 例如,采用以下2个DataFrame
(注意Index
形状的不同):
In [01]: d1 = pandas.DataFrame( numpy.random.randn(15, 4),
columns = ['a', 'b', 'c', 'd'],
index = pandas.DatetimeIndex(start = '01/01/2001',
freq = 'b',
periods = 15)
)
In [02]: d2 = pandas.DataFrame( numpy.random.randn(17, 4),
columns = ['e', 'f', 'g', 'h'],
index = pandas.DatetimeIndex(start = '01/05/2001',
freq = 'b',
periods = 17)
)
我想在这里构造的交叉Index
上加入这两个DataFrame
,例如my_index
:
In [03]: ind = range(0, 10, 2)
In [04]: my_index = d2.index[ind].copy()
因此,以下结果应该具有相同的结果:
In [05]: d1.loc[my_index, :].join(d2.loc[my_index, :] )
Out[65]:
a b c d e f \
2001-01-05 1.702556 -0.885554 0.766257 -0.731700 -1.071232 1.806680
2001-01-09 -0.968689 -0.700311 1.024988 -0.705764 0.804285 -0.337177
2001-01-11 1.249893 -0.613356 1.975736 -0.093838 0.428004 0.634204
2001-01-15 0.430000 0.502100 0.194092 0.588685 -0.507332 1.404635
2001-01-17 1.005721 0.604771 -2.296667 0.157201 1.583537 1.359332
g h
2001-01-05 -1.183528 1.260880
2001-01-09 0.352487 0.700853
2001-01-11 1.060694 0.040667
2001-01-15 -0.044510 0.565152
2001-01-17 -0.731624 -0.331027
因为这是一个更大的应用程序,我将有任意数量的DataFrame
,我想:
pandas
功能而不是构建我自己的hack,即reduce( map ( ) )
等。 DataFrame
交集的视图,而不是创建DataFrame
的副本 我认为没有开箱即用的Pandas功能。 但是,建立自己的并不难:
def select_join(dfs, index):
result = dfs[0].reindex(index)
for df in dfs[1:]:
result = result.join(df, how='inner')
return result
例如,
import numpy as np
import pandas as pd
import string
import itertools as IT
columns = iter(string.letters)
dfs = []
for i in range(3):
d1 = pd.DataFrame( np.random.randn(15, 4),
columns = list(IT.islice(columns, 4)),
index = pd.DatetimeIndex(start = '01/01/2001',
freq = 'b',
periods = 15))
dfs.append(d1)
ind = range(0, 10, 2)
my_index = d1.index[ind].copy()
print(select_join(dfs, my_index))
产量
a b c d e f \
2001-01-01 0.228430 -1.154375 -0.612703 -2.760826 -0.877355 -0.071581
2001-01-03 1.452750 1.341027 0.051486 1.231563 0.428353 1.320172
2001-01-05 -0.966979 -1.997200 -0.376060 -0.692346 -1.689897 0.549653
2001-01-09 -0.117443 -0.888103 2.092829 -0.467220 -1.083004 -1.443015
2001-01-11 -0.168980 -0.152663 0.365618 0.444175 -1.472091 -0.578182
g h i j k l
2001-01-01 -0.098758 0.920457 -1.072377 -0.627720 0.223060 0.903130
2001-01-03 1.962124 1.134501 -0.209813 -2.309090 0.358121 0.655156
2001-01-05 1.088195 -1.705393 -0.161167 -0.339617 0.945495 0.220701
2001-01-09 0.970829 1.931192 0.943150 -1.895580 0.815188 -1.485206
2001-01-11 0.747193 -1.221069 -0.164531 -0.395197 -0.754051 0.922090
关于第二个考虑因素:如果index
是任意的,则无法返回视图。 DataFrame将数据(类似dtype)存储在NumPy数组中。 从NumPy数组中选择任意行时,将分配新数组的空间,并将行从原始数组复制到新数组中。 仅当选择可以表示为基本切片时才返回视图。 NumPy的这种限制 - 一个非常难以删除的限制! - 冒泡到Pandas,导致DataFrames在索引不能表达为基本切片时返回副本。
我已经接受了@ unutbu的答案,但我认为展示我创建的两个函数(和@ unutbu)及其不同的%timeit
值以防任何人想要使用它可能是有价值的:
df_list
和my_index
: dfs = []
for i in range(5):
tmp = pandas.DataFrame( numpy.random.randn(1000, 4),
columns = list(itertools.islice(columns, 4)),
index = pandas.DatetimeIndex(start = '01/01/2000',
freq = 'b',
periods = 1000)
)
dfs.append(tmp)
ind = range(0, 1000, 2)
my_index = tmp.index[ind].copy()
def join_on_index_a(df_list, index):
return pandas.concat(
map( lambda x: x.reindex(index), df_list),
axis = 1
)
#@unutbu's implementation
def join_on_index_b(df_list, index):
result = dfs[0].reindex(index)
for df in dfs[1:]:
result = result.join(df, how='inner')
return result
def join_on_index_c(df_list, index):
return pandas.concat( map( lambda x: x.loc[index, :], df_list), axis = 1)
%timeit
的结果 In [49]: %timeit join_on_index_a(dfs, my_index)
1000 loops, best of 3: 1.85 ms per loop
In [50]: %timeit join_on_index_b(dfs, my_index)
100 loops, best of 3: 1.94 ms per loop
In [51]: %timeit join_on_index_c(dfs, my_index)
100 loops, best of 3: 21.5 ms per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.