使用 Pandas 将 DataFrame 中某些列和行的值替换为同一 DataFrame 中另一列的值
Replace values of certain column and rows in a DataFrame with the value of another column in the same dataframe with Pandas
我想将索引 155 到 304 的“categori”列中的值替换为具有相同索引的“keterangan”列,这是我的数据集:
我已经试过了,但是没用
# 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']
这将仅在从 155
到 305
的索引上将值从 keterangan
复制到 categori
我想将索引 155 到 304 的“categori”列中的值替换为具有相同索引的“keterangan”列,这是我的数据集:
我已经试过了,但是没用
# 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']
这将仅在从 155
到 305
keterangan
复制到 categori