I have a list
name as list_1
like this:
[["xx ",'yy']
[["gg ",'xx']
[["xx ",'gg']]
Also, I have a df
like this:
column1 column2............ coulymn_N
0 xx gg gg
1 gg xx yy
2 xx something else nan
I want to search line by line the list_1
into the df
and return the matching
So for example an output for the first line of list_1
inside df
could be
column1
0 xx
and also
column_N
1 yy
The problem that I have is that in my list
i have more than one value ("xx ",
and "yy")
and so I am confusing. I do not think so that I can use the isin
method.
Any ideas?
CodePudding user response:
Suppose you have a list like:
list_of_lists = [["xx", "yy"], ["gg", "xx"], ["xx", "gg"]]
and a pandas dataframe like this:
import pandas as pd
df = pd.DataFrame(
{
"column1": ["xx", "yy", "aa"],
"column2": ["yy", "gg", "aa"],
"column3": ["gg", "aa", "hh"],
}
)
You can then go through each sub_list
of the list_of_lists
and check if each item of the sub_list exists in any of the columns of the dataframe. In this case you can print the result:
for sub_list in list_of_lists:
print("\n")
print("Sub_list: ", sub_list)
for item in sub_list:
for col in df.columns:
if item in df[col].values:
print(df[col][df[col] == item].to_frame())
The above would print the following:
Sub_list: ['xx', 'yy']
column1
0 xx
column1
1 yy
column2
0 yy
Sub_list: ['gg', 'xx']
column2
1 gg
column3
0 gg
column1
0 xx
Sub_list: ['xx', 'gg']
column1
0 xx
column2
1 gg
column3
0 gg