Pandas 使用其他值创建一个新列

Pandas creating a new column with value from other

希望你一切都好,感谢你的时间和帮助。

我的问题是:

我想根据以下数据在数据框中创建一个新列: 如果 df["A"] == df ["B"] 上的值,则 df["new"] 是 df["B"]

像这样:

   A     B      C
   100   100   colors
   100   10021  Blue
   100   10022  Red
.
.
.
   200   200    Shape
   200   20021  Square
   200   20022  Circle

我需要的是一个新专栏,在 df["A"] == df["B"] 中,df["new"] = colors..

        A     B      C    new
   100   100   colors    colors
   100   10021  Blue     colors
   100   10022  Red      colors
.
.
.
   200   200    Shape    shape
   200   20021  Square   shape
   200   20022  Circle   shape

如果两列中的相同值始终是组中的第一个,则可以使用 Series.where 来处理不相同值的缺失值,然后通过 ffill:

向前填充它们
df['new'] = df['C'].where((df["A"] == df ["B"])).ffill()
print (df)
     A      B       C     new
0  100    100  colors  colors
1  100  10021    Blue  colors
2  100  10022     Red  colors
3  200    200   Shape   Shape
4  200  20021  Square   Shape
5  200  20022  Circle   Shape