Reputation: 48
I have two pandas series of the same length but with different column names. How can one add the values in them?
series.add(other, fill_value=0, axis=0)
does avoid NaN
-values, but the values are not added. Instead, the result is a concatenation of the two series.
Is there a way to obtain a new series consisting of the sum of the values in two series?
Upvotes: 2
Views: 1622
Reputation: 164623
This issue is your 2 series have different indices. Here's an example:
s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)
print(s1.add(s2, fill_value=0, axis=0))
0 1.0
1 NaN
2 3.0
3 NaN
4 5.0
10 NaN
11 7.0
12 8.0
13 NaN
14 NaN
dtype: float64
You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.
You can use a dictionary to realign. The mapping below is arbitrary. NaN
values occur where, after reindexing, values in both series are NaN
:
index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
s2.index = s2.index.map(index_map)
print(s1.add(s2, fill_value=0, axis=0))
0 1.0
1 NaN
2 10.0
3 NaN
4 13.0
dtype: float64
In this case, you can either construct a new series with the regular pd.RangeIndex
as index (i.e. 0, 1, 2, ...
), or use an index from one of the input series:
# normalized index
res = pd.Series(s1.values + s2.values)
# take index from s1
res = pd.Series(s1.values + s2.values, index=s1.index)
Upvotes: 3
Reputation: 64298
The values
attribute lets you access the underlying raw numpy arrays. You can add those.
raw_sum = series.values + other.values
series2 = Series(raw_sum, index=series.index)
This also works:
series2 = series + other.values
Upvotes: 1