How-to articles, tricks, and solutions about DATAFRAME

Change column type in pandas

In pandas, you can change the data type of a column using the astype() function.

Combine two columns of text in pandas dataframe

In pandas, you can use the str.cat() function to combine the values of two columns of text into a single column.

Convert floats to ints in Pandas?

To convert floats to integers in Pandas, you can use the astype() function.

Convert list of dictionaries to a pandas DataFrame

Here is an example of how to convert a list of dictionaries to a pandas DataFrame:

Convert pandas dataframe to NumPy array

In pandas, you can convert a DataFrame to a NumPy array by using the values attribute.

Convert Python dict into a dataframe

You can use the Pandas library to convert a Python dictionary into a dataframe.

Converting a Pandas GroupBy output from Series to DataFrame

Here is an example code snippet that demonstrates how to convert the output of a Pandas GroupBy operation from a Series to a DataFrame:

Create a Pandas Dataframe by appending one row at a time

Here is an example of creating a Pandas DataFrame by appending one row at a time:

Creating an empty Pandas DataFrame, and then filling it

Note that in above example, the DataFrame is created empty first, and then columns are added one by one using the assignment operator (=).

Delete a column from a Pandas DataFrame

You can delete a column from a Pandas DataFrame using the drop function.

Deleting DataFrame row in Pandas based on column value

In Pandas, you can delete a row in a DataFrame based on a certain column value by using the drop() method and passing the index label of the row you want to delete.

Filter pandas DataFrame by substring criteria

Here is an example of how you can filter a pandas DataFrame by substring criteria:

Get a list from Pandas DataFrame column headers

You can use the DataFrame.columns attribute to access the column labels of a DataFrame as an Index object.

Get list from pandas dataframe column or row?

In Pandas, a DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

In pandas, you can use the groupby() method to group data by one or more columns and then use the agg() method to compute various statistics for each group.

How are iloc and loc different?

iloc and loc are both used to select rows and columns from a Pandas DataFrame, but they work differently.

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 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 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 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.

1 2