简体   繁体   中英

setting column of dataframe by index of slice of same dataframe in pandas not working

I'm trying to set values of column for some rows of dataframe based on index of dataframe view. This does not work as expected. What are altrenatives?

i tried following code:

df=pd.DataFrame({'a':[1,2,3]})
dfb=df[df.a>1]
df['c']=np.nan
df.loc[dfb[dfb.a>2].index]['c']=10
df.loc[dfb[dfb.a>2].index]

expected that c column of df.loc[dfb[dfb.a>2].index] would be 10. it is still nan

尝试将df.loc[dfb[dfb.a>2].index]['c']=10更改为df.loc[dfb[dfb.a>2].index,'c']=10

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