简体   繁体   English

通过熊猫中相同数据帧的切片索引设置数据帧的列不起作用

[英]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。它仍然是nan

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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