简体   繁体   English

如果两列的值不同,则在 dataframe 中创建新行

[英]Create new row in a dataframe if values from two columns are different

Lets say I have a dataframe like this one:假设我有一个像这样的 dataframe:

     Col1   Col2   Tag_history   New_tag      Col5           created
0  Name1    Value1    Tag10      Tag10       Rank4     2021-03-21 12:58:09
1  Name1    Value2    Tag10      Tag10       Rank4     2021-03-21 13:58:09
2  Name1    Value3    Tag10      Tag10       Rank4     2021-03-21 14:58:09
3  Name2    Value1    Tag8       Tag9        Rank1     2021-03-21 10:58:09
4  Name2    Value2    Tag8       Tag9        Rank1     2021-03-21 11:58:09
5  Name2    Value4    Tag8       Tag9        Rank1     2021-03-21 12:58:09
6  Name2    Value5    Tag8       Tag9        Rank1     2021-03-21 13:58:09

So, i want to compare columns Tag_history and New tag and if the tag has changed, i want to add a new row that shows in the Tag_history also the new Tag.因此,我想比较列 Tag_history 和新标签,如果标签已更改,我想添加一个新行,该行在 Tag_history 中也显示新标签。 Eg for For Name2, the tag has changed from Tag8 to Tag9, so i want my df to look like this:例如对于 Name2,标签已从 Tag8 更改为 Tag9,所以我希望我的 df 看起来像这样:

   Col1   Col2   Tag_history   New_tag       Col5          created
0  Name1    Value1    Tag10      Tag10       Rank4    2021-03-21 12:58:09
1  Name1    Value2    Tag10      Tag10       Rank4    2021-03-21 13:58:09
2  Name1    Value3    Tag10      Tag10       Rank4    2021-03-21 14:58:09
3  Name2    Value1    Tag8       Tag9        Rank1    2021-03-21 10:58:09
4  Name2    Value2    Tag8       Tag9        Rank1    2021-03-21 11:58:09
5  Name2    Value4    Tag8       Tag9        Rank1    2021-03-21 12:58:09
6  Name2    Value5    Tag8       Tag9        Rank1    2021-03-21 13:58:09
7  Name2    IDLE      Tag9       Tag9        Rank1    2022-01-24 16:50:00 (current datetime)

First of all, I don't recommend using any loops because they are not very effective.首先,我不建议使用任何循环,因为它们不是很有效。

different_value = df[~(df['Tag_history'] == df['New_tag'])] #First check and search for rows that contains different "Tag_history" and "New_tag"

different_value.loc[:,'New_tag'] = different_value['Tag_history']  #Create the new rows

df = df.append(different_value, ignore_index = True) # append dataframes

暂无
暂无

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

相关问题 根据两个不同列中的各自值在 DataFrame 中创建新列 - Create new column in DataFrame based on respective values in two different columns 从熊猫数据框中的唯一行值创建新列 - Create new columns from unique row values in a pandas dataframe 从具有不同值和类型的一列创建新的 dataframe 列 - Create new dataframe columns from one column with different values and types 使用来自不同行的值在 DataFrame 中创建新列 - Create new column in a DataFrame using values from a different row Create a new dataframe from an old dataframe where the new dataframe contains row-wise avergae of columns at different locations in the old dataframe - Create a new dataframe from an old dataframe where the new dataframe contains row-wise avergae of columns at different locations in the old dataframe 如何将 DataFrame 按两列分组,并使用第三列的最小值和最大值创建两个新列? - How to group DataFrame by two columns and create two new columns with min and max values from third column? 在 PySpark 中 - 如果列表中的值位于不同的 DataFrame 的行中,如何在 PySpark 中创建新的 DataFrame? - In PySpark - How to create a new DataFrame in PySpark if values from list are in row of a different DataFrame? 将函数应用于pandas数据帧的每一行以创建两个新列 - Apply function to each row of pandas dataframe to create two new columns Pandas dataframe 使用基于上述行的值创建新列 - Pandas dataframe create new columns with values based on above row 在 dataframe 中创建一个新列,其中包含列表中该行的其他列的值 - Create a new column in the dataframe that has values of other columns for that row in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM