Home > Software design >  how can i use fillna(inplace=True) in for loop?
how can i use fillna(inplace=True) in for loop?

Time:11-03

df = pd.read_csv('IMDB-Movie-Data.csv')

dp = df.groupby('Director')['Revenue (Millions)'].mean()
for i in dp.index:
    df.loc[df['Director']==i,'Revenue (Millions)'].fillna(dp[i],inplace = True)

sorry for bad English
dp is Revenue column's mean values grouped by Director column.
dp.index means Directors
i dont know why that code is not working.
i want nan values in Revenue columns to be changed to mean values that is grouped by Directors
Help me....
there is no errors but not working....squid games...

CodePudding user response:

try this :

df = pd.read_csv('IMDB-Movie-Data.csv')

dp = df.groupby('Director')['Revenue (Millions)'].mean()
df['Revenue (Millions)'].fillna(df.groupby('Director')['Revenue (Millions)'].transform('mean'),inplace = True)
  • Related