Reputation: 4807
I have a list of pandas.core.series.Series
. The size of list is 10. Each series in the list has an index and a value.
I want to intersect the 10 pandas.core.series.Series
in the list to obtain one final DataFrame of all values with common index as the new index. It should have 10 columns for values corresponding to elements in the original list.
Is there a pythonic way to do this instead of using series intersection multiple times?
Upvotes: 3
Views: 2395
Reputation: 294586
Consider the list of series los
los = [
pd.Series([1, 2, 3], [0, 2, 4], name='A'),
pd.Series([1, 2, 3], [1, 2, 4], name='B'),
pd.Series([1, 2, 3], [4, 3, 2], name='C')
]
Using pd.concat
with parameter join='inner'
'inner'
is required to get the intersection of indices
pd.concat(los, axis=1, join='inner')
A B C
4 3 3 1
2 2 2 3
Had we not used join='inner'
pd.concat(los, axis=1)
A B C
0 1.0 NaN NaN
1 NaN 1.0 NaN
2 2.0 2.0 3.0
3 NaN NaN 2.0
4 3.0 3.0 1.0
Which is not the intersection
Upvotes: 3
Reputation: 10243
The command pd.concat
is intended specifically for this. Note that, in my example, the indexes are all the same but pd.concat
will intersect indexes automatically.
## generate data
series_list = [pd.Series(np.random.randn(10)) for x in range(10)]
## output example
In [13]: pd.concat(series_list, axis=1)
Out[13]:
0 1 2 3 4 5 6 \
0 0.859256 -2.283672 0.539067 -0.745864 -2.658162 0.353576 0.482614
1 0.999823 0.155528 -0.579598 0.356863 -0.135860 -0.406351 0.888127
2 -0.727666 -1.571515 0.639486 0.394803 0.478038 0.244531 -1.422910
3 0.582662 1.469884 -3.337026 -0.407253 -2.351327 -0.676233 0.018020
4 1.173179 0.211506 -0.360274 -0.299976 -0.479131 1.735279 0.549555
5 -1.589117 -2.037131 -1.843098 0.066606 0.166258 -1.444472 1.534016
6 -0.275819 -0.978669 2.299632 0.807746 -1.358762 1.190374 -0.668204
7 0.933350 -0.536032 1.285759 0.677269 -0.385078 -0.357661 2.085237
8 0.167977 0.090950 1.220945 1.085571 -0.486978 0.848816 -0.559023
9 -1.006001 -0.168631 1.501675 -0.351409 1.719402 0.337982 -0.776788
7 8 9
0 0.015773 2.356775 -1.288013
1 1.292615 1.272147 0.347335
2 1.410002 -0.364822 -0.372589
3 -2.306940 -0.816853 2.565389
4 -1.815764 -1.547376 1.104517
5 -1.561681 -0.373882 0.582264
6 -1.272563 -0.317298 -0.446855
7 1.179011 -1.402293 0.424124
8 -1.839095 1.278204 -1.166991
9 0.950620 0.681596 -1.908053
Upvotes: 1