How-to articles, tricks, and solutions about 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.

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.

Selecting multiple columns in a Pandas dataframe

To select multiple columns in a pandas DataFrame, you can pass a list of column names to the indexing operator [].

Set value for particular cell in pandas DataFrame using index

In pandas, you can set the value of a specific cell in a DataFrame using the at method.

Use a list of values to select rows from a Pandas dataframe

You can use the .loc property of a Pandas dataframe to select rows based on a list of values.

Writing a pandas DataFrame to CSV file

In the above code snippet, the to_csv method is used to write a DataFrame to a CSV file.

1 2