*EDIT Also doesn't work with .loc
I've been hesitant to create yet another post about fillna not working as there's already many available. But I've been stuck for a good day working around this.
I'm using python with pandas and numpy and have a dataframe I'm using fillna on with a list comprehension. While there's most likely some opportunities for better performance. I'm having trouble with the fillna method not working. I could take this logic out of a list comprehension and put it in a embedded loop. For time, I'd rather not.
Why are the NaN values not being updated?
Thanks ahead of time.
df_race_details['MSTR_ID'] = np.nan
lst_b_cust = []
df_grpd_cust = df_race_details.groupby('CUST_LIST_ID')
itr_mstr_id = iter(range(1, len(df_grpd_cust.groups)+1,1))
[
df_race_details.loc[df_race_details['CUST_LIST_ID'] == nm_b, 'MSTR_ID'].fillna(value=itr_mstr_id.__next__(), inplace=True)
for nm_a, gp_a in df_grpd_cust
if gp_a['MSTR_ID'].isnull().values.any()
for nm_b, gp_b in df_grpd_cust
if gp_b['MSTR_ID'].isnull().values.any()
for i in lst_gp_b
if i in gp_a['CUSTOMER_ID'].tolist()
]
#Specifically this line is not updating the dataframe
df_race_details.loc[df_race_details['CUST_LIST_ID'] == nm_b, 'MSTR_ID'].fillna(value=itr_mstr_id.__next__(), inplace=True)
You should try something like this because sometimes this approach worked for me.
df_race_details.loc[df_race_details['CUST_LIST_ID'] = df_race_details.loc[df_race_details['CUST_LIST_ID'] == nm_b, 'MSTR_ID'].fillna(value=itr_mstr_id.__next__())
Make sure that, you have removed inplace attribute.
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.