考虑list
dfs
of pd.DataFrame
s
import pandas as pd
import numpy as np
np.random.seed([3,1415])
dfs = [pd.DataFrame(np.random.rand(10, 2),
columns=['Col1', 'Col2']) for _ in range(5)]
我会用pd.concat
to join
原始连接
堆栈值而不考虑它来自哪里
pd.concat([d.Col1.loc[d.Col2.gt(.8)] for d in dfs], ignore_index=True)
0 0.850445
1 0.934829
2 0.879891
3 0.085823
4 0.739635
5 0.700566
6 0.542329
7 0.882029
8 0.496250
9 0.585309
10 0.883372
Name: Col1, dtype: float64
加入源信息
使用keys
范围
pd.concat([d.Col1.loc[d.Col2.gt(.8)] for d in dfs], keys=range(len(dfs)))
0 3 0.850445
5 0.934829
6 0.879891
1 1 0.085823
2 0.739635
7 0.700566
2 4 0.542329
3 3 0.882029
4 0.496250
8 0.585309
4 0 0.883372
Name: Col1, dtype: float64
另一种方法
use query
pd.concat([d.query('Col2 > .8').Col1 for d in dfs], keys=range(len(dfs)))
0 3 0.850445
5 0.934829
6 0.879891
1 1 0.085823
2 0.739635
7 0.700566
2 4 0.542329
3 3 0.882029
4 0.496250
8 0.585309
4 0 0.883372
Name: Col1, dtype: float64