Categories
Python Answers

How to bin a column with Python Pandas?

To bin a column with Python Pandas, we can use the cut method.

For instance, we werite

bins = [0, 1, 5, 10, 25, 50, 100]
df['binned'] = pd.cut(df['percentage'], bins)

to add the binned column by calling cut with the data frame df‘s percentage column with the bins.

Categories
Python Answers

How to sort a dataFrame in Python Pandas by two or more columns?

To sort a dataFrame in Python Pandas by two or more columns, we call sort_values with the ascending argument.

For instance, we write

df.sort_values(['a', 'b'], ascending=[True, False])

to call sort_values on the df data frame with an array of columns to sort as the first argument.

And we set ascending to an array of booleans to sort the columns in the order listed for each column.

Categories
Python Answers

How to change the order of Python Pandas DataFrame columns?

To change the order of Python Pandas DataFrame columns, we can pyut the columns in a different order in a brackets.

For instance, we write

df = df[['mean', '0', '1', '2', '3']]

to add an array with the column names we want in the brackets to reorder them as listed.

And then we reassign the returned dataframe to df.

Categories
Python Answers

How to create a cartesian product in Python Pandas?

To create a cartesian product in Python Pandas, we call the data frame merge method with how set to 'cross'.

For instance, we write

from pandas import DataFrame
df1 = DataFrame({'col1':[1,2],'col2':[3,4]})
df2 = DataFrame({'col3':[5,6]})    

df1.merge(df2, how='cross')

to call df1.merge with df2 and the how argument set to 'cross' to return cartesian product between df1 and df2.

Categories
Python Answers

How to select multiple columns in a Python Pandas dataframe?

To select multiple columns in a Python Pandas dataframe, we can use df.loc.

For instance, we write

df.loc[:, 'C':'E']

to select all columns between 'C' and 'E' inclusive.