Reputation: 17933
Suppose I have a DataFrame
df = pandas.DataFrame({'a': [1,2], 'b': [3,4]}, ['foo', 'bar'])
a b
foo 1 3
bar 2 4
And I want to added a column based on another Series
:
s = pandas.Series({'foo': 10, 'baz': 20})
foo 10
baz 20
dtype: int64
How do I assign the Series to a column of the DataFrame and provide a default value if the DataFrame index value is not in the Series index?
I'm looking for something of the form:
df['c'] = s.withDefault(42)
Which would result in the following Dataframe:
a b c
foo 1 3 10
bar 2 4 42
#Note: bar got value 42 because it's not in s
Thank you in advance for your consideration and response.
Upvotes: 2
Views: 846
Reputation: 294358
map
with get
get
has an argument that you can use to specify the default value.
df.assign(c=df.index.map(lambda x: s.get(x, 42)))
a b c
foo 1 3 10
bar 2 4 42
reindex
with fill_value
df.assign(c=s.reindex(df.index, fill_value=42))
a b c
foo 1 3 10
bar 2 4 42
Upvotes: 3