简体   繁体   中英

change specific columns of data frame using pandas

I have a data frame 3 columns: [" date ", " volume ", " ID "]. ID=0,1,2...15. I would like to create new data frame: keep all rows with ID=5 .

Other rows: still keep them all, but set the row["volume"] = 0 .

First copy your dataframe:

df_new = df.copy()

Then, using pd.DataFrame.loc , set volume to 0 for your criteria:

df_new.loc[df_new['ID'] != 5, 'volume'] = 0

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