Issue
I have below code
import pandas as pd
dat1 = pd.DataFrame({'col1' : ['A', 'B', 'A', 'C'], 'col2' : ['Z', 'Z', 'X', 'Y']})
Use_pipe = dat1.pipe(lambda x : x or x.to_csv('aa.csv'))
The last line fails. Is there any way to make it work i.e. simultaneously passing values to Use_pipe
and save the dataframe using pipe
?
Solution
One option is with pyjanitor's also function:
# pip install pyjanitor
import janitor
import pandas as pd
dat1.also(lambda df: df.to_csv('aa.csv')).pipe('whatever function')
You can also just define a function and use that in your pipe:
def return_df(df, func, *args, **kwargs):
func(df.copy(), *args, **kwargs)
return df
dat1.return_df(lambda df: df.to_csv('aa.csv')).pipe(wateva)
Another way would be to use a tuple, coupled with an index (hacky):
dat1.pipe(lambda df: (df, df.to_csv('aaa.csv'))[0]).pipe(wateva)
Answered By - sammywemmy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.