简体   繁体   English

根据另一列pandas将列设置为等于值

[英]Setting column equal to value depending on another column pandas

I am stuck on how to set the value of solvent column in each row to a number in the num column in the data frame. 我坚持如何将每行中溶剂列的值设置为数据框中num列中的数字。 eg I need num to be equal to 9 when solvent is Nonane and num equal to 8 when solvent is Octane etc. Any help would be great. 例如,当溶剂是壬烷时,我需要num等于9,当溶剂是辛烷等时,我需要等于8,任何帮助都会很好。

在此输入图像描述

use .loc with a boolean mask 使用.loc和布尔掩码

df.loc[df['solvent'] == 'NONANE', 'num'] = 9
df.loc[df['solvent'] == 'OCTANE', 'num'] = 8

Another method is do define a dict and call map : 另一种方法是定义一个dict和调用map

d = {'NONANE':9, 'OCTANE':8, 'HEPTANE':7, 'HEXANE':6}
df['num'] = df['solvent'].map(d)

暂无
暂无

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

相关问题 dataframe 的列中存在的子集字符串,具体取决于另一列的值 - Pandas - Subsetting strings present in a column of a dataframe, depending on value of another column - Pandas 根据 pandas 中的另一个列值更改列值 - Change column values depending a another column value in pandas 如果 pandas 列值等于另一列的值,则更新它们 - Update pandas column values if they are equal to another column's value 如何根据另一列中的值将函数应用于Pandas中的列? - How to apply a function to a column in Pandas depending on the value in another column? 根据 pandas 中另一列的值计算列中 integer 值的数量 - Counting the number of integer values in a column depending on the value of another column in pandas 如果等于另一列中的值,则熊猫从多列返回值 - Pandas return value from multiple columns if equal to value in another column Pandas:使一列的值等于另一列的值 - Pandas: Make the value of one column equal to the value of another 根据熊猫数据框中的另一列设置列值 - Setting a column value based on another column in a pandas dataframe Pandas:创建新列并根据字符串列中的值(子字符串)和另一列上的值添加值 - Pandas: Create new column and add value depending on value (substring) in a string column and value on another column 根据索引使Pandas Dataframe列等于另一个Dataframe中的值 - Make Pandas Dataframe column equal to value in another Dataframe based on index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM