简体   繁体   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

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复制到仅在索引155305上的categori

暂无
暂无

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

相关问题 如何在pandas DataFrame中通过共享列值替换某些行? - How to replace certain rows by shared column values in pandas DataFrame? 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas Pandas数据框:根据另一列中的值替换多行 - Pandas dataframe: Replace multiple rows based on values in another column 大熊猫:通过列的值提取某些行作为数据框 - pandas: extract certain rows as a dataframe by the value of a column Pandas 将列的值替换为与另一个 Dataframe 的比较 - Pandas replace values of a column with comparison to another Dataframe Determining Values in Pandas Dataframe 基于另一列中的前几行值 - Determining Values in Pandas Dataframe Based on Previous Rows Value in Another Column Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe 在Pandas数据框中查找具有相同列值的行 - Finding rows with same column values in pandas dataframe 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe Pandas Dataframe:在同一列的值中根据特定范围替换列中的值 - Pandas Dataframe: Replace values in a column based on certain Range in values of the same column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM