Home > other >  Why <name 'pd' is not defined> error occured?
Why <name 'pd' is not defined> error occured?

Time:10-17

When I run this code in the google colab,

import pandas as pd
import I_2

df = pd.read_csv("data.csv")
result = I_2.I2(df)
print(result)

<name 'pd' is not defined> error occured. What's wrong with that code?

I_2 module's function I2's code is start like this..

def I2(df1):
    df2 =pd.DataFrame(df1, columns=["A", "B", "C", "D"] )
    num =len(df2)

CodePudding user response:

The line import pandas as pd should also appear at the beginning of S_2.py, because your import before import S_2 is not visible from within the S_2 module.

CodePudding user response:

In your S_2 module, you have to import pandas too.

import pandas as pd

def p2(df1):
    df2 =pd.DataFrame(df1, columns=["A", "B", "C", "D"] )
    ...
  • Related