簡體   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