繁体   English   中英

将 DataFrame 中某些列和行的值替换为同一 dataframe 和 Pandas 中的另一列的值

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

我想用具有相同索引的“keterangan”列替换“categori”列中从索引 155 到 304 的值,这是我的数据集的样子:

在此处输入图像描述

我已经尝试过了,但它不起作用

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

这可以使它工作

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

这会将值从keterangan复制到仅在索引155305上的categori

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM