Ashok
Ashok

Reputation: 214

Python: Pandas data-frame inside a function

I would like to call pandas dataframe values inside a function. A simplified example of my requirement is: "print the values of a data frame using a (defined) python function"

Working example:

Defining a dataframe:

import pandas as pd
s = pd.Series(range(1,10))

Then I defined a function to print the values one by one:

def write(f,n1,n2):
    for n in range(n1,n2):
        print(n,f) 

After that the function is called:

write(s.loc[n],3,8)

But this gives an error "name 'n' is not defined". Please help me to fix this.

Upvotes: 0

Views: 1882

Answers (1)

A.Kot
A.Kot

Reputation: 7903

Got a little bit of mixup:

def write(f,n1,n2):
for n in range(n1,n2):
    print(f.loc[n])

write(s,3,8)

Upvotes: 1

Related Questions