'how to fill a row in a subcolumn inside a multi column dataframe?

I have a multicolumn dataframe called full_week that the first column is the employees names and the other columns are columns with each weekday name starting from Sunday to Thursday. Each weekday column has 5 more subcolumns for different kinds of employee shift preferences. a visual explanation of the employee column next to one-weekday column for example:

|Employee |Sunday
___________|_____________________________|

|In|Out|Hours|Weekday|Workplace|

In the beginning, the dataframe will only have NaN values. When i retrieve the employee input from the GUI I created about the shift information and stored it inside a dictionary, I want to take 1 row from the column above, for example, the row with index 0, and fill the NaN values with the dictionary that has the same keys as the sub column names.

the dictionary: dd = {'In': '09:00:00', 'Out': '17:00:00', 'Hours': 8, 'Weekday': 'Tuesday', 'Workplace': 'Office'}

I tried this code using fillna with the dictionary above but the changes are not saving, even when I write inplace=True:

full_week['Sunday'].iloc[0].fillna(dd, inplace=True)

I think the problem is about taking a slice from the dataframe but can't find the right solution to only fill the specific row of the specific chosen weekday column.

I have this function that eventually what you wrote returns None:

def mapping_the_request():
    global row
    global day
    choices = show_chosen_info() #--> the input from the GUI
    cols = ['Analyst','In', 'Out', 'Hours', 'Weekday', 'Workplace']
    d = dict(zip(cols, choices))
    
    for index, col in full_week.iterrows():
        if col[0] == d['Analyst'] and d["Weekday"] in full_week.columns:
            day = d['Weekday']
            row = index
            
    del d['Weekday'] #--> Deleting because i don't need it anymore.
    del d['Analyst'] #--> Deleting because i don't need it anymore.
    full_week.loc[full_week.index.get_loc(row), day] = full_week.loc[full_week.index.get_loc(row), day].fillna(d)
    return full_week

full_week.update(mapping_the_request())


Solution 1:[1]

The first problem is that you're calling the .fillna method on the pandas.Series that is returned by your indexing and the inplace option can't really work as you intended. What you should do is to reassign the Series back to the Dataframe.

The second problem is that doing the indexing as you did it, you'll get a copy of a slice, see the Pandas doc for more information.

To achieve your goal, you should instead do:

full_week.loc[full_week.index.get_loc(0), 'Sunday'] = 
            full_week.loc[full_week.index.get_loc(0), 'Sunday'].fillna(dd)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David