You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using groupby().apply() to compute new columns in a dataframe, for example like this:
df1=DataFrame([{"val1": 1, "val2" : 20}, {"val1":1, "val2": 19}, {"val1":2, "val2": 27}, {"val1":2, "val2": 12}])
deffunc(dataf):
returndataf["val2"] -dataf["val2"].mean()
printtype(df1.groupby("val1").apply(func)) # this is a Seriesdf1["centered"] =df1.groupby("val1").apply(func)
printdf1
However, if the set of values of the grouped by column ("val1") is unique, the groupby above returns a dataframe as opposed to a Serie, in which case the assignment of the result to a column fails:
df2=DataFrame([{"val1": 1, "val2" : 20}, {"val1":1, "val2": 19}, {"val1":1, "val2": 27}, {"val1":1, "val2": 12}])
deffunc(dataf):
returndataf["val2"] -dataf["val2"].mean()
printtype(df2.groupby("val1").apply(func)) # this is a DataFramedf2["centered"] =df2.groupby("val1").apply(func) # this fails: cannot assign a DataFrame to a columnprintdf2
As a result, my code is littered by check of uniqueness of grouped by parameter:
I am using groupby().apply() to compute new columns in a dataframe, for example like this:
However, if the set of values of the grouped by column ("val1") is unique, the groupby above returns a dataframe as opposed to a Serie, in which case the assignment of the result to a column fails:
As a result, my code is littered by check of uniqueness of grouped by parameter:
Am I taking the wrong approach there? If not, would it be possible to have a consistent returned type?
Thanks!
The text was updated successfully, but these errors were encountered: