0
import pandas as pd
df = pd.read_csv("path", sep = " ")
newdf = df.loc[:,:].isin(["(#10)","#9","Automation","Create","log"])
print(newdf)

This is my code. I have a big logfile where I want to look for different error codes ["(#10)","#9","Automation","Create","log"] in the whole file.

The file has 16124 rows and 70 columns. The code itself works, but the problem I have is that the output is only "True" and "False". I want the output to be the actual words from the file. Can anyone help me?

The poutput I get is:

1 2 3 4 5 6 7 8 ...
0 False False False False True False False False ...
1 False False False False False False False False ...
2 False False False False False False False False ...
3 False False False False False False False True...

[16124 rows x 70 columns]

The output I want it all rows with the error codes in every column.

1 2 3 4 5 6 7 8 ...
3 2022-06-21 14:20:25 65891 DEBUG Concern #8 nonlog ...
8 2022-06-21 14:20:25 65891 INFO Automation #8 nonlog ...
71 2022-06-21 14:20:25 65891 INFO Concern #9 nonlog ...
358 2022-06-21 14:20:25 65891 INFO Concern #8 log ...

[4 rows x 70 columns]

Steffi Ri
  • 1
  • 1
  • `df.where(newdf)`? See [pd.DataFrame.where](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.where.html). You'll get your original df with values where condition is `True`. Where condition is `False`, you'll get `NaN` values by default. But you can change them into something else with the `other` parameter, e.g.: `df.where(newdf, other="empty")`. If you are looking for a different (perhaps more specific) output, please be more specific in your question, and add some sample *input* and *expected output* data. – ouroboros1 Jul 05 '22 at 07:18
  • You may try searching on a specific column `df.loc[df['specific_column_to_search_in'].isin(["(#10)","#9","Automation","Create","log"])]` this should give you the desired result – Sanidhya Singh Jul 05 '22 at 07:22
  • @SanidhyaSingh It works like you said for one column. But I have about 70 columns... – Steffi Ri Jul 05 '22 at 07:38
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 05 '22 at 07:46
  • @SteffiRi is this helpful? https://stackoverflow.com/questions/26640129/search-for-string-in-all-pandas-dataframe-columns-and-filter – Sanidhya Singh Jul 05 '22 at 07:52

2 Answers2

0

You can try this

import numpy as np

mask = np.column_stack([df.loc[:,:].isin(["(#10)","#9","Automation","Create","log"])])
df.loc[mask.any(axis=1)]

adapted from this answer Search for String in all Pandas DataFrame columns and filter

Sanidhya Singh
  • 441
  • 3
  • 11
  • thanks, this worked for me now: `df[ df.select_dtypes(object) .apply(lambda row: row.str.contains("with"), axis=1) .any(axis=1) ` – Steffi Ri Jul 05 '22 at 08:31
0

You can try out this method

newdf = df[df.isin(["(#10)","#9","Automation","Create","log"])].dropna()
print(newdf)