How-to articles, tricks, and solutions about PANDAS

How can I get a value from a cell of a dataframe?

You can use the .at or .iat methods to get the value of a specific cell in a DataFrame.

How do I change the size of figures drawn with Matplotlib?

To change the size of figures drawn with Matplotlib in Python, you can use the figure() function and set the figsize argument.

How do I count the NaN values in a column in pandas DataFrame?

You can use the isna() function to create a boolean mask of the NaN values in a column, and then use the sum() function to count the number of True values in the mask.

How do I expand the output display to see more columns of a Pandas DataFrame?

You can expand the output display of a Pandas DataFrame by setting the option 'display.max_columns' in pandas.

How do I get the row count of a Pandas DataFrame?

You can use the shape property of the DataFrame to get the number of rows and columns.

How do I select rows from a DataFrame based on column values?

You can use the DataFrame.loc method to select rows from a DataFrame based on column values.

How to add a new column to an existing DataFrame?

You can add a new column to an existing pandas DataFrame by using the assign() method or the [] notation.

How to add an empty column to a dataframe?

In pandas, you can add an empty column to a DataFrame using the assign() method or the insert() method.

How to apply a function to two columns of Pandas dataframe

To apply a function to two columns of a Pandas DataFrame, you can use the apply() method of the DataFrame and pass the function as an argument.

How to change the order of DataFrame columns?

To change the order of columns in a Pandas DataFrame, you can use the DataFrame's "reindex" method and specify the new order of the columns.

How to check if any value is NaN in a Pandas DataFrame

You can use the isna() method to check for NaN values in a Pandas DataFrame.

How to convert index of a pandas dataframe into a column

To convert the index of a pandas DataFrame into a column, you can use the reset_index() function, and specify that you want to move the index to a new column with the inplace=True and name parameter.

How to deal with SettingWithCopyWarning in Pandas

The "SettingWithCopyWarning" in pandas is raised when you try to modify a copy of a DataFrame or Series rather than the original.

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

You can drop rows of a Pandas DataFrame that have a NaN value in a certain column using the dropna() function.

How to iterate over rows in a DataFrame in Pandas

You can use the iterrows() method to iterate over rows in a Pandas DataFrame.

How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

You can replace NaN values in a column of a Pandas Dataframe by using the fillna() method and passing in the value you want to replace NaN with.

how to sort pandas dataframe from one column

To sort a Pandas DataFrame based on the values in a column, you can use the sort_values() method of the DataFrame.

Pandas DataFrame Groupby two columns and get counts

Here is an example code snippet that demonstrates how to use the groupby() method in pandas to group a DataFrame by two columns and get the counts for each group:

Pandas index column title or name

To set the name of the index column in a pandas DataFrame, you can use the .rename_axis() method or the .index.name attribute.

Pandas Merging 101

Here is an example of how to use the pd.merge() function to merge two DataFrames in pandas:

Pretty-print an entire Pandas Series / DataFrame

You can use the .head() method to print the first few rows of a Pandas Series or DataFrame in a "pretty" format.

Python Pandas: Get index of rows where column matches certain value

You can use the .loc method to filter the DataFrame and get the boolean mask, and then use the .index property to get the index of the rows that match the certain value.

Renaming column names in Pandas

To rename the column names of a Pandas DataFrame, you can use the DataFrame.rename() method.

Selecting a row of pandas series/dataframe by integer index

You can use the .iloc[] property to select a row by its integer index in a pandas DataFrame or Series.