0

I have to achieve the below scenario, I am very new to Python I have a method which takes a data frame and split that data frame into two data frames based on some conditions. Ex:

def splitdf(df:DataFrame) {
      return [df1, df2]
      retuns two data frames    // how can we return two data frames if it is list then another method which takes one of these two data frames as an input is failing because its a list.
}

df1, df2 = splitdf(dftest) // this assignment is failing

need to pass these df1 and df2 as a data frame to some other methods like below

calculate(df1)
calculate(df2)

since above line are failing, I am doing below

df=splitdf(dftest)
calculate(df[0])
calculate(df[1])

// the problem here is calculate method is failing because it is expecting parameter as data frame but if I am passing like df[0] it is taking it as a list only. I need to send two data frames to calculate() method separately after splitting the data in splitdf().

Example:

data = {'id':[1,2,3,4],
        'Name':['Tom', 'Jack', 'Steve', 'Ricky'],
        'Age':[28,34,29,42],
        'Addr':['addr1,'addr2','addr3','addr4'],
        'mobile':['mob1','mob2','mob3','mob4'],
        'sex':['M','M','M','M']
}
dftest = pd.DataFrame(data)

suppose above data frame splitted into by calling splitdf(dftest)

def splitdf(dftest) {
    //some code to split dftest
    return [df1, df2]  //method has to return two data frames after 
                         splitting. after splitting consider df1 with 
                         columns id, name, age, sex. df2 with columns 
                         id, addr, mobile.
}

dfs = splitdf(dftest)

Now want to pass these two data frames dfs[0], df[1] to calculate() method (which accepts DataFrame as parameter) to do processing further.

But if we are passing like this calculate(dfs[0]) //dfs[0] is going as list only not dataframe. I want to pass as DataFrame.

Hmm
  • 105
  • 10
  • Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, a complete listing on any error messages beginning with the word traceback, and the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. That being said, your function splitdf is not returning two dataframes. Use the debugger or a print statement to see what it is producing. – itprorh66 Oct 19 '22 at 13:33
  • Your error is hidden somewhere behind `//some code to split dftest`. Please note that comments in python are introduced by `#` and span a single line. It is rather jarring to read to see `//` used. Additionally, please post enough code so that we can run it and see the error, but also make sure to edit out parts that are unimportant. – Steven Rumbalski Oct 19 '22 at 14:08
  • splitdf() method is not giving any error (// given just for understanding)and it is working fine and returning two dataframes as a list but calculate method is giving me error because its expecting DataFrame but I am passing dfs[0] or dfs[1], calculate method is considering this dfs[0] as a list only even though I am returning it as dataframe in list. – Hmm Oct 19 '22 at 14:33
  • @Hmm If `calculate()` sees `dfs[0]` as a list then it's a list. That means that `splitdf()` is not returning two dataframes despite your claims to the contrary. I suppose it's also possible that the body of `calculate` contains a line that assigns over a dataframe argument with a list. Either way, we need to see actual code from those functions that produces the error. – Steven Rumbalski Oct 19 '22 at 15:06
  • Before calling `calculate()` what does `print(type(dfs[0])` show? – Steven Rumbalski Oct 19 '22 at 19:33
  • type(dfs[0]) showing its a data frame. Maybe it is something wrong with the Prefect. I am calling splitdf() from prefect flow. splitdf() method is annotated with @task – Hmm Oct 20 '22 at 04:37

0 Answers0