Rishikesh Kumar Rishi has Published 1162 Articles

Delete the first three rows of a DataFrame in Pandas

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 13:30:13

915 Views

To delete the first three rows of a DataFrame in Pandas, we can use the iloc() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Delete the first three rows using df.iloc[3:].Print the updated DataFrame.Example import pandas as pd df = pd.DataFrame(    {     ... Read More

How to convert a DataFrame into a dictionary in Pandas?

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 13:22:41

1K+ Views

To convert a Pandas DataFrame into a dictionary, we can use the to_dict() method. Let's take an example and see how it's done.StepsCreate two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Convert the DataFrame into a dictionary using to_dict() method and print it.Example import pandas as pd ... Read More

How to put a Pandas DataFrame into a JSON file and read it again?

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 13:18:13

893 Views

To put a Pandas DataFrame into a JSON file and read it again, we can use to_json() and read_json() methods.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use to_json() method to dump the DataFrame into a JSON file.Use read_json() method to read the JSON file.Exampleimport pandas ... Read More

Select DataFrame rows between two index values in Python Pandas

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 12:57:27

5K+ Views

We can slice a Pandas DataFrame to select rows between two index values. Let's take an example and see how it's done.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable for lower limit of the index.Initialize another variable for upper limit of the index.Use ... Read More

Selecting with complex criteria from a Pandas DataFrame

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 12:40:37

108 Views

We can use different criteria to compare all the column values of a Pandas DataFrame. We can perform comparison operations like df[col]2, then it will check all the values from col and compare whether they are greater than 2. For all the column values, it will return True if the ... Read More

Count unique values per groups in Python Pandas

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 12:07:37

6K+ Views

To count unique values per groups in Python Pandas, we can use df.groupby('column_name').count().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use df.groupby('rank')['id'].count() to find the count of unique values per groups and store it in a variable "count".Print the count from Step 3.Exampleimport pandas as pd ... Read More

Group-by and Sum in Python Pandas

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 11:59:23

5K+ Views

To find group-by and sum in Python Pandas, we can use groupby(columns).sum().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the groupby sum using df.groupby().sum(). This function takes a given column and sorts its values. After that, based on the sorted values, it also sorts the ... Read More

How to get column index from column name in Python Pandas?

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 14-Sep-2021 11:45:05

12K+ Views

To get column index from column name in Python Pandas, we can use the get_loc() method.Steps −Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Find the columns of DataFrame, using df.columns.Print the columns from Step 3.Initialize a variable column_name.Get the location, i.e., of index for column_name.Print ... Read More

How to find the common elements in a Pandas DataFrame?

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 07-Sep-2021 08:08:53

5K+ Views

To find the common elements in a Pandas DataFrame, we can use the merge() method with a list of columnsStepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.Print the input DataFrame, df1.Create another two-dimensional tabular data, df2.Print the input DataFrame, df2.Find the common elements using merge() method.Print the common DataFrame.Exampleimport ... Read More

Combining two Series into a DataFrame in Pandas

Rishikesh Kumar Rishi

Rishikesh Kumar Rishi

Updated on 30-Aug-2021 12:23:44

2K+ Views

To combine two series into a DataFrame in Pandas, we can take two series and concatenate them using concat() method.StepsCreate series 1 with two elements, where index is ['a', 'b'] and name is Series 1.Print Series 1.Make Series 2 with two elements, where index is ['a', 'b'] and name is Series 2.Print Series 2.Concatenate Pandas ... Read More

Advertisements