Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to search a value and return the row from multiple dataframes in pandas?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading