King D Anonymous
King D Anonymous

Reputation: 105

How to loop a function in a other function in Python Jupyter Notebook?

My desired code is:

def clustering_data(n):
      for i in n:
            di = dd(i)
            DF = [d0, d1, d2, d3,...,d(n-1)]
            df = pd.concat(DF)
      return df

Here,

For example, if n is 5, my desire that the loop would generate following processes:

def clustering_data(5):
      for i in 5:
            d0 = dd(0)
            d1 = dd(1)
            d2 = dd(2)
            d3 = dd(3)
            d4 = dd(4)
            DF = [d0, d1, d2, d3, d4]
            df = pd.concat(DF)
      return df

Actually, I would like to run the dd[i] function in i=1 to i=n times. Each loop will generate di data set (d0,d1,d2,....). Then I will combine the all di data-sets (d0,d1,d2....).

I need your valuable opinion and suggestion in this regard.

N.B.: dd(value) is a function which need one integer to execute.

And this is my first question in the Stack Overflow. I apologize for any inconvenience.

Upvotes: 0

Views: 556

Answers (1)

augray
augray

Reputation: 3161

Python's great! Your desired pseudocode is almost directly translatable to valid syntax:

def clustering_data(n):
    DF = [dd(i) for i in range(n)]
    df = pd.concat(DF)
    return df

That is, assuming I'm understanding your intentions correctly. The above will make df the result of concatenating n data frames. Your pseudocode would have produced sum(i for i in range(n)) data frames to concat.

Some tips:

  • Look up list comprehensions
  • The fact that you're in a Jupyter notebook doesn't change how this function works (leaving that out of your question description would likely attract a wider set of potential answer-ers)

Upvotes: 1

Related Questions