Issue
While reading this article, I came across this statement.
order_total = df.groupby('order')["ext price"].sum().rename("Order_Total").reset_index()
Other than reset_index()
method call, everything else is clear to me.
My question is what will happen if I don't call reset_index()
considering the given below sequence?
order_total = df.groupby('order')["ext price"].sum().rename("Order_Total").reset_index()
df_1 = df.merge(order_total)
df_1["Percent_of_Order"] = df_1["ext price"] / df_1["Order_Total"]
I tried to understand about this method from https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html, but couldn't understand what does it mean to reset the index of a dataframe.
Solution
I think better here is use GroupBy.transform
for new Series
with same size like original DataFrame filled by aggregate values, so merge
is not necessary:
df_1 = pd.DataFrame({
'A':list('abcdef'),
'ext price':[5,3,6,9,2,4],
'order':list('aaabbb')
})
order_total1 = df_1.groupby('order')["ext price"].transform('sum')
df_1["Percent_of_Order"] = df_1["ext price"] / order_total1
print (df_1)
A ext price order Percent_of_Order
0 a 5 a 0.357143
1 b 3 a 0.214286
2 c 6 a 0.428571
3 d 9 b 0.600000
4 e 2 b 0.133333
5 f 4 b 0.266667
My question is what will happen if I don't call reset_index() considering the sequence?
Here is Series
before reset_index()
, so after reset_index
is converting Series
to 2 columns DataFrame, first column is called by index name and second column by Series
name.
order_total = df_1.groupby('order')["ext price"].sum().rename("Order_Total")
print (order_total)
order
a 14
b 15
Name: Order_Total, dtype: int64
print (type(order_total))
<class 'pandas.core.series.Series'>
print (order_total.name)
Order_Total
print (order_total.index.name)
order
print (order_total.reset_index())
order Order_Total
0 a 14
1 b 15
Reason why is necessry in your code to 2 columns DataFrame is no parameter in merge
. It means it use parameter on
by intersection of common columns names between both DataFrames, here order
column.
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.