I have a pandas dataframe and one of the columns ‘sigResponseActions’ has the following data in it
[{'@odata.type': '#microsoft.graph.security.collectInvestigationPackageResponseAction', 'identifier': 'deviceId'}, {'@odata.type': '#microsoft.graph.security.runAntivirusScanResponseAction', 'identifier': 'deviceId'}, {'@odata.type': '#microsoft.graph.security.stopAndQuarantineFileResponseAction', 'identifier': 'deviceId,sha1'}]
How do I search for all rows in the dataframe that have ‘stopAndQuarantineFileResponseAction’ or ‘softDeleteResponseAction’ ??
This gives no results…
strings = ['stopAndQuarantineFileResponseAction', 'softDeleteResponseAction']
filtered_df = df[df['SigResponseActions'].isin(strings)]
filtered_df
This gives no results…
df1 = df[df['SigResponseActions'].str.contains('stopAndQuarantineFileResponseAction',na=False)]
df1.head(5)
I’d appreciate any help…and REALLY appreciate any explanation WHY this doesn’t work.
Thank you!
3
Assuming this input with a column of dictionaries:
df = pd.DataFrame({'SigResponseActions': [{'@odata.type': '#microsoft.graph.security.collectInvestigationPackageResponseAction', 'identifier': 'deviceId'}, {'@odata.type': '#microsoft.graph.security.runAntivirusScanResponseAction', 'identifier': 'deviceId'}, {'@odata.type': '#microsoft.graph.security.stopAndQuarantineFileResponseAction', 'identifier': 'deviceId,sha1'}]})
You need to loop over the values in the dictionaries and the target strings:
strings = ['stopAndQuarantineFileResponseAction', 'softDeleteResponseAction']
out = df.loc[[any(s in v for v in d.values() for s in strings) for d in df['SigResponseActions']]]
Output:
SigResponseActions
2 {'@odata.type': '#microsoft.graph.security.stopAndQuarantineFileResponseAction', 'identifier': 'deviceId,sha1'}
try this:
mask = df.astype('str')['SigResponseActions'].str.contains('|'.join(strings), regex=True)
filtered_df = df.loc[mask]
print(filtered_df)