pandas mutate new column

Solutions on MaxInterview for pandas mutate new column by the best coders in the world

showing results for - "pandas mutate new column"
Greta
16 Nov 2017
1#Using .assign you can make multiple
2#operations that depend on the
3#previous ones without the need
4#of creating intermediate variables
5import pandas as pd
6
7df = pd.DataFrame({
8    'name': ['alice','bob','charlie','daniel'],
9    'age': [25,66,56,78]
10})
11
12df.assign(
13    is_senior = lambda dataframe: dataframe['age'].map(lambda age: True if age >= 65 else False) 
14).assign(
15    name_uppercase = lambda dataframe: dataframe['name'].map(lambda name: name.upper()),
16).assign(
17    name_uppercase_double = lambda dataframe: dataframe['name_uppercase'].map(lambda name: name.upper()+"-"+name.upper())
18)
19