0

I have a function that is essentially something like this

def Yes_No(factor):
    factor_yes = ####  --> Dataframe
    factor_no = ####   --> Dataframe
return (factor_yes, factor_no)

If factor = 'Male', and I want to view the dataframe Male_yes, how can I do this? I have multiple factors I want to analyse, it's just I don't know how to name the output in a way that I can call each of the dataframes individually. Or should I just create 2 functions?

'factor' here is some feature from my main dataframe. Each feature is a column of Yes and No. So I want to output 1 dataframe that is filtered by Yes and another dataframe that is filtered by No.

Rex Low
  • 2,069
  • 2
  • 18
  • 47
user10939484
  • 167
  • 2
  • 13
  • Assuming you mean using pandas, this is a duplicate of [Pandas split DataFrame by column value](https://stackoverflow.com/questions/33742588/pandas-split-dataframe-by-column-value). It's totally unnecessary to have a function, or to return a tuple of dataframes. If all you want to do is use a variable to index between different columns, use logical indexing like the pandas tutorial shows e.g. `df[df.gender=='Male']` – smci Feb 22 '19 at 03:31
  • Like I said, I have multiple factors so I want to keep things clean by creating a function if I can. Right now, I have things filtered through query, but it'd be nice to have a function. Basically the same code for all the different factors. – user10939484 Feb 22 '19 at 04:00
  • smci showed the cleanest way to grab a subset... – gregV Feb 22 '19 at 04:07
  • **To split a df on a column's values then iterate over both resulting dataframes, use [`pandas groupby()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html) instead**. But if you really insist on doing this, can you supply a few lines more code for context, show us why it would look cleaner? There's no need to lower performance by wrapping a single line in a function call, unless you want to parameterize the column names and values. – smci Feb 22 '19 at 21:35

0 Answers0