find all - 查找数据框的一列与其他匹配模式的所有匹配项,并获得相应的值
find all - to find all occurrence of matching pattern one column of a data frame to other and get the corresponding value
我正在处理一个需求,有 2 个 CSV 如下 -
CSV.csv
Short Description Category
Device is DOWN! Server Down
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Disk Space Is Lowon ;E: Disk Space Utilization
Disk Space Is Lowon;C: Disk Space Utilization
Network Interface Down Interface Down
and reference.csv
Category Complexity
Server Down Simple
Network Interface down Complex
Drive Cleanup Windows Medium
CPU Utilization Medium
Memory Utilization Medium
Disk Space Utilization Unix Simple
Windows Service Restart Medium
UNIX Service Restart Medium
Web Tomcat Instance Restart Simple
Expected Output
Short Description Category Complexity
Device is DOWN! Server Down Simple
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Disk Space Is Lowon ;E: Disk Space Utilization Medium
Disk Space Is Lowon;C: Disk Space Utilization Medium
Network Interface Down Interface Down Complex
现在,我需要查询 CSV1.csv
并选择 'Category'
的值,并在 reference.csv
的 Category
列中查找所有可能的匹配项并获得相应的 'Complexity'
来自 reference.csv
并将数据放在 CSV1.csv
.
的每个类别中
我正在使用 find.all 来实现这一点。我无法按预期那样做。有没有更好的方法来达到同样的效果。
我尝试使用 disct
函数,但没有给出预期的结果。
一种可能的方法:
my_dict = dict(zip(reference_df['Category'].values, reference_df['Complexity'].values))
def match_key(key, default_value):
for d_key in my_dict.keys():
if key in d_key or d_key in key:
return my_dict[d_key]
return default_value
CSV1_df['Complexity'] = CSV1_df['Category'].apply(lambda x: match_key(x, 'default'))
解释:
- 通过压缩参考
Dataframe
中的 Category 和 Complexity 列构建一个 dict
,即 {'Server Down': 'Simple', 'Network Interface down': 'Complex'...}
- 使用
apply
和 lambda
函数使用每个 Category 从字典中获取相应的 Complexity 值CSV1 中的值 Dataframe
作为键
- 我们定义一个函数来查找 CSV1 Category 中的值
Dataframe
是否是字典中任何键的子字符串,反之亦然,并在 apply
- 将其保存到 CSV1 中的新列
Dataframe
我正在处理一个需求,有 2 个 CSV 如下 -
CSV.csv
Short Description Category
Device is DOWN! Server Down
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Device Performance Alerts was triggered on Physical memory Memory Utilization
Disk Space Is Lowon ;E: Disk Space Utilization
Disk Space Is Lowon;C: Disk Space Utilization
Network Interface Down Interface Down
and reference.csv
Category Complexity
Server Down Simple
Network Interface down Complex
Drive Cleanup Windows Medium
CPU Utilization Medium
Memory Utilization Medium
Disk Space Utilization Unix Simple
Windows Service Restart Medium
UNIX Service Restart Medium
Web Tomcat Instance Restart Simple
Expected Output
Short Description Category Complexity
Device is DOWN! Server Down Simple
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
CPU Warning Monitoron XSSXSXSXSXSX.com CPU Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Device Performance Alerts was triggered on Physical memory Memory Utilization Medium
Disk Space Is Lowon ;E: Disk Space Utilization Medium
Disk Space Is Lowon;C: Disk Space Utilization Medium
Network Interface Down Interface Down Complex
现在,我需要查询 CSV1.csv
并选择 'Category'
的值,并在 reference.csv
的 Category
列中查找所有可能的匹配项并获得相应的 'Complexity'
来自 reference.csv
并将数据放在 CSV1.csv
.
我正在使用 find.all 来实现这一点。我无法按预期那样做。有没有更好的方法来达到同样的效果。
我尝试使用 disct
函数,但没有给出预期的结果。
一种可能的方法:
my_dict = dict(zip(reference_df['Category'].values, reference_df['Complexity'].values))
def match_key(key, default_value):
for d_key in my_dict.keys():
if key in d_key or d_key in key:
return my_dict[d_key]
return default_value
CSV1_df['Complexity'] = CSV1_df['Category'].apply(lambda x: match_key(x, 'default'))
解释:
- 通过压缩参考
Dataframe
中的 Category 和 Complexity 列构建一个dict
,即{'Server Down': 'Simple', 'Network Interface down': 'Complex'...}
- 使用
apply
和lambda
函数使用每个 Category 从字典中获取相应的 Complexity 值CSV1 中的值Dataframe
作为键 - 我们定义一个函数来查找 CSV1 Category 中的值
Dataframe
是否是字典中任何键的子字符串,反之亦然,并在apply
- 将其保存到 CSV1 中的新列
Dataframe