Categories
Python Answers

How to Format or Suppress Scientific Notation from Python Pandas Aggregation Results?

Sometimes, we want to format or suppress scientific notation from Python Pandas aggregation results, we call set_option to change how numbers are formatted.

In this article, we’ll look at how to format or suppress scientific notation from Python Pandas aggregation results, we call set_option to change how numbers are formatted.

How to Format or Suppress Scientific Notation from Python Pandas Aggregation Results?

To format or suppress scientific notation from Python Pandas aggregation results, we call set_option to change how numbers are formatted.

For instance, we write

pd.set_option('display.float_format', lambda x: '%.3f' % x)

to set the 'display.float_format' option by calling set_option with 'display.float_format' and a lambda function to return the number x formatted our way with a format string.

Conclusion

To format or suppress scientific notation from Python Pandas aggregation results, we call set_option to change how numbers are formatted.

Categories
Python Answers

How to hot encode in Python Pandas?

Sometimes, we want to hot encode in Python Pandas.

In this article, we’ll look at how o hot encode in Python Pandas.

How to hot encode in Python Pandas?

To hot encode in Python Pandas, we an use the get_dummies method.

For instance, we write

import pandas as pd

s = pd.Series(list('abca'))
pd.get_dummies(s)

to create a series from the list with pd.Series.

Then we call get_dummies with series s to convert the categorical variable into dummy/indicator variables.

Conclusion

To hot encode in Python Pandas, we an use the get_dummies method.

Categories
Python Answers

How to remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas?

Sometimes, we want to remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas.

In this article, we’ll look at how to remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas.

How to remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas?

To remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas, we an use the drop_duplicates method.

For instance, we write

df.drop_duplicates(subset='A', keep="last")

to call drop_duplicates on the df data frame with the subset argyments to remove the items in A, while keeping the last values by setting keep to 'last‘.

Conclusion

To remove duplicates by columns A, keeping the row with the highest value in column B with Python Pandas, we an use the drop_duplicates method.

Categories
Python Answers

How to delete rows from a Python Pandas DataFrame based on a conditional expression?

To delete rows from a Python Pandas DataFrame based on a conditional expression, we can use the drop method.

For instance, we write

df = df.drop(df[df.score < 50].index)

to call drop to remove the items from the score column with values less than 50 with

df.drop(df[df.score < 50].index)

and return a new data frame with the removed values.

We can also set the inplace argument to True to do the removal in place:

df.drop(df[df.score < 50].index, inplace=True)
Categories
Python Answers

How to do string concatenation of two Python Pandas columns

To do string concatenation of two Python Pandas columns, we concatenate the columns directly.

For instance, we write

df['bar'] = df.bar.map(str) + " is " + df.foo

to update the bar column with the bar column values converted to strings with map.

And then we concatenate the ' is ' string and the foo column values onto the df.bar values.