简体   繁体   中英

Replace values of certain column and rows in a DataFrame with the value of another column in the same dataframe with Pandas

i want to replace value in "categori" column from index 155 to 304 with "keterangan" column with the same index, here is my dataset looks like:

在此处输入图像描述

i already try this but it doesnt work

# make a variabel with value from 155 to 304
ket_list = list(range(155,305))

# Selecting new value
new = df['keterangan'][ket_list]
new

# Selecting old value
old = df['categori'][ket_list]
old

# replace values of "categori" value with
# "keterangan" value from the same dataframe
df = df.replace(['old'],'new')

This can make it work

df.loc[155:305,'categori'] = df['keterangan']

This will copy the values from keterangan to categori only on the index from 155 to 305

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