Reputation: 33
count_lbl = pd.DataFrame(labels_dataframe.sum(axis=1) \
.sort_values(ascending=False)) \
.reset_index() \
.groupby(0).count() \
.reset_index() \
.rename(columns={0:'num_lbl','index':'count'})
I am fairly new to Python and i would like to know if this is the best way of writing a long line of code, with multiple sequenced actions on an object.
Upvotes: 2
Views: 158
Reputation: 19230
The other answer's approach is to use an extra pair of parentheses. That's fine.
There is also an alternative that just reorganize the lines to take advantage of existing parentheses.
count_lbl = pd.DataFrame(
labels_dataframe.sum(axis=1).sort_values(ascending=False)
).reset_index().groupby(0).count().reset_index().rename(
columns={0:'num_lbl','index':'count'}
)
In particular:
Upvotes: 2
Reputation: 13061
You can use left and right parentheses
count_lbl = (pd.DataFrame(labels_dataframe.sum(axis=1)
.sort_values(ascending=False))
.reset_index()
.groupby(0).count()
.reset_index()
.rename(columns={0:'num_lbl','index':'count'}))
Upvotes: 5