Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas pivot_table apply aggfunc last instance

Tags:

python

pandas

I have made a pivot table with various columns and have applied aggfunc like np.sum and first and count. I want last instance of corresponding value of a column from a dataframe. Is there any function that could serve this purpose?

like image 746
stormtrooper12 Avatar asked Jul 28 '16 10:07

stormtrooper12


People also ask

How do I pivot multiple columns in Pandas?

Using the Pandas pivot_table() function we can reshape the DataFrame on multiple columns in the form of an Excel pivot table. To group the data in a pivot table we will need to pass a DataFrame into this function and the multiple columns you wanted to group as an index.

How do I reset the index after pivot Pandas?

How to Reset an Index in Pandas. Pandas comes built in with a handy dataframe method, the . reset_index() method, that lets you, well, reset a Pandas dataframe's index.

How do you reshape a data frame?

melt() function is used to reshape a DataFrame from a wide to a long format. It is useful to get a DataFrame where one or more columns are identifier variables, and the other columns are unpivoted to the row axis leaving only two non-identifier columns named variable and value by default.


1 Answers

I think you can use

aggfunc='last'

Sample:

df = pd.DataFrame({ 'Age':[35, 37, 40, 29, 31, 26, 28],
                   'City':['B', 'Ch', 'LA', 'Ch', 'B', 'B', 'Ch'],
                   'Position':['M','M','M','P', 'P','M','M']})
print (df)
   Age City Position
0   35    B        M
1   37   Ch        M
2   40   LA        M
3   29   Ch        P
4   31    B        P
5   26    B        M
6   28   Ch        M

print (df.pivot_table(index='Position', columns='City', values='Age', aggfunc='last'))
City         B    Ch    LA
Position                  
M         26.0  28.0  40.0
P         31.0  29.0   NaN
like image 76
jezrael Avatar answered Sep 22 '22 21:09

jezrael