Home > Blockchain >  Replace values of certain column and rows in a DataFrame with the value of another column in the sam
Replace values of certain column and rows in a DataFrame with the value of another column in the sam

Time:12-22

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 :

enter image description here

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')

CodePudding user response:

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

  • Related