Assuming a multiindex dataframe as follows:
import pandas as pd
import numpy as np
arrays = [np.array(['John', 'John', 'John', 'Jane', 'Jane', 'Jane']),
np.array(['New York', 'New York', 'San Francisco', 'New York', 'New York', 'San Francisco']),
np.array(['2018', '2019', '2018', '2019', '2020', '2020']),
np.array(['HR', 'Finance', 'HR', 'Finance', 'HR', 'Finance']),
np.array(['Company A', 'Company A', 'Company A', 'Company B', 'Company B', 'Company B']),
np.array(['Manager 1', 'Manager 1', 'Manager 2', 'Manager 2', 'Manager 3', 'Manager 3'])]
df = pd.DataFrame(np.random.randn(3, 6), columns=arrays)
df.columns.names = ['userid', 'city', 'year', 'department', 'company', 'line_manager']
display(df)
(The real case is much bigger of course)
I need to change the values of the names in the level called userid based on a function.
Here an example:
def change_name(name):
if name.starts_with("J"):
# only changing the name under certain conditions
result = "name"+ "00"
else:
result = name
return result
How do I do that? The rest of the indexes values remain the same.