我有下面的代码可以工作,但是UserWarning在打印数据时它会抛出一些错误。
import pandas as pd
pd.set_option('display.height', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('expand_frame_repr', True)
data = pd.read_csv('/home/karn/plura/Test/Python_Pnada/Cyber_July.csv', usecols=['Platform ID', 'Safe', 'Target system address', 'Failure reason'])
hostData = data[data['Platform ID'].str.startswith("CS-Unix-")][data['Safe'].str.startswith("CS-NOI-DEFAULT-UNIX-ROOT")] [['Platform ID', 'Safe', 'Target system address','Failure reason']]
hostData.reset_index(level=0, drop=True)
print(hostData)
Run Code Online (Sandbox Code Playgroud)
以下是用户警告..
./CyberCSV.py:12: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
hostData = data[data['Platform ID'].str.startswith("CS-Unix-")][data['Safe'].str.startswith("CS-NOI-DEFAULT-UNIX-ROOT")] [['Platform ID', 'Safe', 'Target system address','Failure reason']]
Run Code Online (Sandbox Code Playgroud)
其次,有没有办法像我一样在数据框中使用通配符
data['Safe'].str.startswith("CDS-NOI-DEFAULT-UNIX-ROOT")我想在哪里使用data['Safe'].str.startswith("CDS-*DEFAULT-UNIX-ROOT")
这可能吗。
您可以链接startswith和endswith掩码或使用contains- ^is 匹配字符串的开头,.*is 匹配任何字符串和$结尾:
mask = data['Safe'].str.startswith("CDS") & data['Safe'].str.endswith("DEFAULT-UNIX-ROOT")
Run Code Online (Sandbox Code Playgroud)
或者正则表达式:
mask = data['Safe'].str.contains("^CDS-.*DEFAULT-UNIX-ROOT$")
Run Code Online (Sandbox Code Playgroud)
样本:
data = pd.DataFrame({'Safe':['CDS-DEFAULT-UNIX-ROOT',
'CDS-NhjghOI-DEFAULT-UNIX-ROOT',
'CDS-NhjghOI-DEFAULT',
'ACDS-DEFAULT-UNIX-ROOT']})
print (data)
Safe
0 CDS-DEFAULT-UNIX-ROOT
1 CDS-NhjghOI-DEFAULT-UNIX-ROOT
2 CDS-NhjghOI-DEFAULT
3 ACDS-DEFAULT-UNIX-ROOT
mask = data['Safe'].str.contains("^CDS-.*DEFAULT-UNIX-ROOT$")
print (mask)
0 True
1 True
2 False
3 False
Name: Safe, dtype: bool
Run Code Online (Sandbox Code Playgroud)