简体   繁体   中英

how can i use fillna(inplace=True) in for loop?

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...

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM