简体   繁体   English

遍历DataFrame并使用null值更新列

[英]Iterate through a DataFrame and update column with value if it is null

I am attempting to loop through a Dataframe and update the column 'GROUP_NAME' with 'REG' where it is null. 我正在尝试遍历数据框,并用“ REG”将列“ GROUP_NAME”更新为空。

for index, row in trax_df.iterrows():
    if row['GROUP_NAME']==None:
        trax_df.loc[index, trax_df['GROUP_NAME']] = 'REG'

I used the above code and it does not give a traceback but it does not update any of the values with 'REG' where 'GROUP_NAME' is Null. 我使用了上面的代码,它没有提供追溯,但是它没有使用'REG'更新任何值,其中'GROUP_NAME'为Null。 What am I missing here? 我在这里想念什么?

EDIT: 编辑:

It has to be a loop to account for future development and eventually I will have to extract the digits out of another column, call it columnB, and concatenate the digits with 'REG'. 必须考虑到未来的发展,这是一个循环,最终我将不得不从另一列中提取数字,将其称为columnB,并将这些数字与'REG'连接起来。 Therefore, I believe I need the index from the Dataframe to be able to do that. 因此,我相信我需要来自Dataframe的索引才能做到这一点。

And there are NaNs in the data. 并且数据中存在NaN。

try this: 尝试这个:

trax_df.loc[trax_df.GROUP_NAME.replace('', np.nan).isnull(), 'GROUP_NAME'] = 'REG'

this will include both empty string and NaNs 这将包括空字符串和NaN

if you don't have NaNs you can simply do this: 如果您没有NaN,则可以执行以下操作:

trax_df.loc[trax_df.GROUP_NAME== '', 'GROUP_NAME'] = 'REG'

是None还是空字符串?

trax_df.loc[(trax_df.GROUP_NAME.isnull() | (trax_df.GROUP_NAME = ''), 'GROUP_NAME'] = 'REG'

暂无
暂无

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

相关问题 遍历数据框中的行并根据其他列更改列的值 - Iterate through rows in a dataframe and change value of a column based on other column 遍历 dataframe 中的每一行和每一列并对列值执行操作 - iterate through each row and column in dataframe and perform action on column value 如何遍历数据框中的列并同时更新两个新列? - How to iterate through a column in dataframe and update two new columns simultaneously? 遍历 dataframe 并根据列值执行代码 - iterate through dataframe and execute code base on column value 遍历嵌套字典以创建 Dataframe 并添加新列值 - Iterate Through Nested Dictionary to Create Dataframe and Add New Column Value 遍历 DataFrame 列中的值,如果满足条件则更新所述值 - Iterate over values in DataFrame column, and update said value if condition is met Pandas 遍历一个数据帧,将行值和列值连接到一个关于特定列值的新数据帧中 - Pandas-iterate through a dataframe concatenating row values and column values into a new dataframe with respect to a specific column value 迭代数据帧并选择空值 - Iterate through dataframe and select null values 遍历pandas数据框,使用if语句检查每个列值,并将该列值传递到空df的首选列 - Iterate through a pandas dataframe, check each column value with an if statement and pass the column values to the prefered column of an empty df 遍历列以检查值 - Iterate through column to check for value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM