Categories
Python Answers

How to drop a list of rows from a Python Pandas dataframe?

Sometimes, we want to drop a list of rows from a Python Pandas dataframe.

In this article, we’ll look at how to drop a list of rows from a Python Pandas dataframe.

How to drop a list of rows from a Python Pandas dataframe?

To drop a list of rows from a Python Pandas dataframe, we can use the data frame drop method.

For instance, we write

df.drop(df.index[[1, 3]])

to call df.drop to drop the items with index between 1 and 3 with df.index[[1, 3].

A new data frame will be returned with the rows dropped.

Conclusion

To drop a list of rows from a Python Pandas dataframe, we can use the data frame drop method.

Categories
Python Answers

How to make tick labels font size smaller with Python Matplotlib?

Sometimes, we want to make tick labels font size smaller with Python Matplotlib.

In this article, we’ll look at how to make tick labels font size smaller with Python Matplotlib.

How to make tick labels font size smaller with Python Matplotlib?

To make tick labels font size smaller with Python Matplotlib, we call tick_params with the labelsize argument.

For instance, we write

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.tick_params(axis='both', which='major', labelsize=10)
ax.tick_params(axis='both', which='minor', labelsize=8)

to call tick_params with the labelsize argument set to the size of the labels on the axes.

Conclusion

To make tick labels font size smaller with Python Matplotlib, we call tick_params with the labelsize argument.

Categories
Python Answers

How to change the figure size of a seaborn axes or figure level plot with Python?

Sometimes, we want to change the figure size of a seaborn axes or figure level plot with Python.

In this article, we’ll look at how to change the figure size of a seaborn axes or figure level plot with Python.

How to change the figure size of a seaborn axes or figure level plot with Python?

To change the figure size of a seaborn axes or figure level plot with Python, we call the set method to set the 'figure.figsize' option.

For instance, we write

import seaborn as sns

sns.set(rc={'figure.figsize':(11.7, 8.27)})

to call set with the rc argument set to a dict with the 'figure.figsize' option to the width and height of the figure in a tuple.

Conclusion

To change the figure size of a seaborn axes or figure level plot with Python, we call the set method to set the 'figure.figsize' option.

Categories
Python Answers

How to install python modules without root access with Python?

Sometimes, we want to install python modules without root access with Python.

In this article, we’ll look at how to install python modules without root access with Python.

How to install python modules without root access with Python?

To install python modules without root access with Python, we can set the package to install in a directory that we have write access to.

For instance, we run

pip install --install-option="--prefix=$HOME/local" package_name

to install the package_name package with the --install-option set to "--prefix=$HOME/local" to install the package in the local directory in the home directory.

Conclusion

To install python modules without root access with Python, we can set the package to install in a directory that we have write access to.

Categories
Python Answers

How to pass a list as a command-line argument with Python argparse?

Sometimes, we want to pass a list as a command-line argument with Python argparse.

In this article, we’ll look at how to pass a list as a command-line argument with Python argparse.

How to pass a list as a command-line argument with Python argparse?

To pass a list as a command-line argument with Python argparse, we can use the add_argument to add the argument.

For instance, we write

parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True)

to call add_argument with the argument flag’s short and long forms.

We set nargs to '+' to let us take one or more argument for the flag.

And then we set help to a string with the help text for the flag.

Conclusion

To pass a list as a command-line argument with Python argparse, we can use the add_argument to add the argument.