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 import and use my own function from .py file in Python Pandas?

In Jupyter Notebook I created my own function in my_fk.py file like below:

import pandas as pd

def missing_val(df):

    df= pd.DataFrame(df.dtypes, columns=["type"])
    df["missing"] = pd.DataFrame(df.isna().any())
    df["sum_miss"] = pd.DataFrame(df.isna().sum())
    df["perc_miss"] = round((df.apply(pd.isna).mean()*100),2)
    
    return df

Then when I try to import and run my function using below code:

    import pandas as pd
    import numpy as np
    import my_fk as fk
df = pd.read_csv("my_data.csv")
    fk.missing_val(df)

I have error like below. Error suggests that in my my_fk.py file there is no pandas as pd, but there IS line with code "import pandas as pd". How can I import and use my own function from python file ?

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

NameError: name 'pd' is not defined

>Solution :

Missing "as". Then place your pd.read_csv() after importing pandas, not before

import pandas as pd
import numpy as np
import my_fk as fk


df = pd.read_csv("my_data.csv")
fk.missing_val(df)
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