我在Python Pandas中有两个DataFrame。
存储在单元格中的数据如下:
DF1
- DatabaseId Integer
- DatabaseName String
DF2
- CreateString String
我想将列DataBaseID应用于DF2中的任何记录,其中DF1.DatabaseName存在于Create String的上下文中。
Example:
DatabaseName = "UserDB" CreateString = "This create string would fail"
DatabaseName = "UserDB" CreateString = "This create string has UserDB in it"
第一条记录将失败,并且不会包含在结果集中。 第二条记录会成功,并且会出现在结果集中。
我研究过各种选项,包括.isin
和.contains
,但这些选项都没有用。这似乎是一个“受控制的”笛卡尔联合,具有'如果匹配成功'的条件。但我无法找到一种方法来做到这一点,而且效率很高。
需要评估的总列表大小分别在100K到500K之间。
更新 添加了更多示例数据:
>>> DF1.head(10)
DatabaseID DatabaseName
0 DB1
1 DB2
2 DB3
3 DB4
...
>>> DF2.head(10)
CreateString
None
None
None
CREATE VIEW DB1.Table1 AS LOC…
None
REPLACE VIEW DB3.Table3...
CREATE VIEW DB3.Table10 AS SELE...
CREATE VIEW DB55.Table999 AS SELEC...
...
Desired Result
DatabaseID DatabaseName CreateText
0 DB1 CREATE VIEW DB1.Table1 AS LOC…
2 DB3 REPLACE VIEW DB3.Table3...
2 DB3 CREATE VIEW DB3.Table10 AS SELE...
...
etc...
...
答案 0 :(得分:1)
更新:如何解析表名:
In [100]: df2['TableName'] = df2.CreateString.str.extract('\s+(\w+\.\w+)\s+', expand=True)
In [101]: df2
Out[101]:
CreateString DatabaseName TableName
0 None NaN NaN
1 None NaN NaN
2 None NaN NaN
3 CREATE VIEW DB1.Table1 AS LOC… DB1 DB1.Table1
4 None NaN NaN
5 REPLACE VIEW DB3.Table3 ... DB3 DB3.Table3
6 CREATE VIEW DB3.Table10 AS SELE... DB3 DB3.Table10
7 CREATE VIEW DB55.Table999 AS SELEC... DB55 DB55.Table999
原始回答:
你可以这样做:
In [83]: df2['DatabaseName'] = df2.CreateString.str.extract('\s+(\w+)\.\w+\s+', expand=True)
In [84]: pd.merge(df2, df1, on='DatabaseName', how='left')
Out[84]:
CreateString DatabaseName DatabaseID
0 None NaN NaN
1 None NaN NaN
2 None NaN NaN
3 CREATE VIEW DB1.Table1 AS LOC… DB1 0.0
4 None NaN NaN
5 REPLACE VIEW DB3.Table3 ... DB3 2.0
6 CREATE VIEW DB3.Table10 AS SELE... DB3 2.0
7 CREATE VIEW DB55.Table999 AS SELEC... DB55 NaN