Home > front end >  How to search a value and return the row from multiple dataframes in pandas?
How to search a value and return the row from multiple dataframes in pandas?

Time:03-22

For example if i have multiple dataframes like df1, df2 and df3. I have a column 'phone_no' in every dataframe. How do i search for a phone_no in every dataframe and return the rows where that dataframe is present?

For example

df_all = [df1, df2, df3]

for i in df_all:
    print(i.loc[i['phone_no'] == 9999999999])

The above code is returning empty output. The output must be the row where the phone_no contains that particular phone number. How to resolve this issue?

CodePudding user response:

Check if this works by comparing phone_no to a string:

df_all = [df1, df2, df3]

for i in df_all:
    print(i.loc[i['phone_no'].astype(str) == '9999999999'])

Maybe you don't need to convert phone_no as str if it's already the case. You have to check:

>>> print(df1['phone_no'].dtype)
object

# OR

>>> print(df1['phone_no'].dtype)
int64

Update

df_all = [df1, df2, df3]
df_filtered = []

for i in df_all:
    df_filtered.append(i.loc[i['phone_no'].astype(str) == '9999999999'])
  • Related