How is a Pandas crosstab different from a Pandas pivot_table?

The main difference between the two is the pivot_table expects your input data to already be a DataFrame; you pass a DataFrame to pivot_table and specify the index/columns/values by passing the column names as strings. With cross_tab, you don’t necessarily need to have a DataFrame going in, as you just pass array-like objects for index/columns/values. … Read more

Pandas rank by column value [duplicate]

Here’s one way to do it in Pandas-way You could groupby on Auction_ID and take rank() on Bid_Price with ascending=False In [68]: df[‘Auction_Rank’] = df.groupby(‘Auction_ID’)[‘Bid_Price’].rank(ascending=False) In [69]: df Out[69]: Auction_ID Bid_Price Auction_Rank 0 123 9 1 1 123 7 2 2 123 6 3 3 123 2 4 4 124 3 1 5 124 2 … Read more

Jupyter notebook display two pandas tables side by side

I have ended up writing a function that can do this: [update: added titles based on suggestions (thnx @Antony_Hatchkins et al.)] from IPython.display import display_html from itertools import chain,cycle def display_side_by_side(*args,titles=cycle([”])): html_str=”” for df,title in zip(args, chain(titles,cycle([‘</br>’])) ): html_str+='<th style=”text-align:center”><td style=”vertical-align:top”>’ html_str+=f'<h2 style=”text-align: center;”>{title}</h2>’ html_str+=df.to_html().replace(‘table’,’table style=”display:inline”‘) html_str+='</td></th>’ display_html(html_str,raw=True) Example usage: df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=[‘A’,’B’,’C’,’D’,]) df2 = … Read more

Python Pandas: drop a column from a multi-level column index?

With a multi-index we have to specify the column using a tuple in order to drop a specific column, or specify the level to drop all columns with that key on that index level. Instead of saying drop column ‘c’ say drop (‘a’,’c’) as shown below: df.drop((‘a’, ‘c’), axis = 1, inplace = True) Or … Read more