[英]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:我想用具有相同索引的“keterangan”列替换“categori”列中从索引 155 到 304 的值,这是我的数据集的样子:
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
这会将值从keterangan
复制到仅在索引155
到305
上的categori
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.