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?
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 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With