【发布时间】:2019-02-13 02:02:53
【问题描述】:
我可能使这个问题过于复杂,但我似乎找不到简单的解决方案。
我有两个 DataFrame。我们称它们为 df1 和 df2。为了保持简单。假设 df1 有一列称为“某些数据”,而 df2 有两列称为“某些数据”和“其他数据”。
例子:
df1
Some Data
"Lebron James 123"
"Lebron James 234"
df2
some data other data
"Lebron James 123 + other text" "I want this in df1["New?"]"
"Michael Jordan" "Doesn't Matter"
所以基本上我想在 df1 中创建一个名为“New?”的新列。如果 df1["Some data"] 在 df2["Some other data"] 中,这个新列(在 df1 中)将显示“New”。但是,如果 df2["some data"] 中没有实例,那么我将 df1["New?"] 设置为 df2["other data"] 中该特定行的值。
运行后想要的结果:
df1
Some Data New?
"Lebron James 123" "I want this in df1["New?"]"
"Lebron James 234" "New"
所以你可以看到The New?列将包括来自其他数据列的特定行的值。 Lebron James 234 在 df2 的某些数据中并不存在,所以它说是新的。
我可以使用.isin() 方法让它说出True 或False,但是不知道如何获取其他df 的索引并从其他数据列中获取值。
谢谢
编辑:
据我所知会起作用
df["New?"] = df1["Some Data"].isin(df2["some data"])
会渲染
df1[“新的?”]
True
False
所以我希望 True 成为“我希望 df1[“New?”] 中的这个,而 False 成为新的
【问题讨论】:
-
如果
df1中的值出现在 df2 的多行中怎么办? -
这对我没有影响。我只关心 df2["some data"] 中是否存在 df1["Some Data"] 的实例。如果没有从 df2["other data"] 中获取该行的值
-
好的,这样就更容易了...还有一个问题,你的
df1呢?似乎你应该在第一行有+ other text,否则它不会在你的输出中产生True