Home > OS >  Return different df based on condition
Return different df based on condition

Time:10-27

Have 2 dataframes (foo and bar), but only want to export the dataframe with more rows to a csv. Unsure of how to populate a function that does this. My assumption is that it could look something like the below, but this only returns blank dataframes for x.

def longer():
    if len(foo) > len(bar):
        return foo
    else:
        return bar

x.apply(longer()).to_csv('x.csv')

Hoping this is a quick and easy answer--thanks so much!

CodePudding user response:

If all you want is to export the larger DataFrame, it seems like this would be a lot simpler:

max(foo, bar, key=len).to_csv('x.csv')

Or if you also want to keep a reference to the larger one for further use:

larger = max(foo, bar, key=len)
larger.to_csv('x.csv')
  • Related