Assign pandas.DataFrame column To Series with Default

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

Answers (2)

piRSquared
piRSquared

Reputation: 294358

Using 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

Use 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

harpan
harpan

Reputation: 8631

You need to use join between df and dataframe which is obtained from s and then fill the NaN with default value, which is 42, in your case.

df['c'] = df.join(pandas.DataFrame(s, columns=['c']))['c'].fillna(42).astype(int)

Output:

    a   b   c
foo 1   3   10
bar 2   4   42

Upvotes: 2

Related Questions