简体   繁体   中英

how to perform a pandas dataframe subsetting using index and assign it a same value?

How to partially split a dataset from row indexes, and column names or column indexes. I tried this df.loc[[1,2,34,'name']] = 100 and df.iloc[np.where(df.loc[,'name']<100)]=100 this do not work. Need help, thank you!

data sample:

>> df

>>    name  lag
  0   100.0 0.000000
  1   80.0  0.011161
  2   255.0 0.022321
  3   93.0  0.033482
  4   100.0  0.044643
  5   9.8  0.055804
  6   29.0  0.066964

If need set index values 1,2,34 and columns name name to 100 use:

df.loc[[1,2,34],'name'] = 100

If need set by mask use:

df.loc[df['name']<100, 'name']=100

#for set another column
df.loc[df['name']<100, 'lag']=100

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