Found 10476 Articles for Python

How to append two DataFrames in Pandas?

Rishikesh Kumar Rishi
Updated on 22-Aug-2023 14:37:33

88K+ Views

To append the rows of one dataframe with the rows of another, we can use the Pandas append() function. With the help of append(), we can append columns too. Let's take an example and see how to use this method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.Print the input DataFrame, df1.Create another DataFrame, df2, with the same column names and print it.Use the append method, df1.append(df2, ignore_index=True), to append the rows of df2 with df2.Print the resultatnt DataFrame.Exampleimport pandas as pd df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]}) df2 = pd.DataFrame({"x": [1, 3], "y": ... Read More

How to get nth row in a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 06-Sep-2023 21:48:02

43K+ Views

To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df.iloc[4] will return the 5th row because row numbers start from 0.StepsMake two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print input DataFrame, df.Initialize a variable nth_row.Use iloc() method to get nth row.Print the returned DataFrame.Exampleimport pandas as pd df = pd.DataFrame( dict( name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], marks=[89, 23, 100, 56, 90], subjects=["Math", "Physics", "Chemistry", "Biology", "English"] ) ) ... Read More

How to find numeric columns in Pandas?

Rishikesh Kumar Rishi
Updated on 15-Sep-2021 06:52:03

7K+ Views

To find numeric columns in Pandas, we can make a list of integers and then include it into select_dtypes() method. Let's take an example and see how to apply this method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Make a list of data type, i.e., numerics, to select a column.Return a subset of the DataFrame's columns based on the column dtypes.Print the column whose data type is int.Example import pandas as pd df = pd.DataFrame( dict( name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], ... Read More

Python Pandas – Find the maximum value of a column and return its corresponding row values

Rishikesh Kumar Rishi
Updated on 27-Aug-2023 13:31:30

29K+ Views

To find the maximum value of a column and to return its corresponding row values in Pandas, we can use df.loc[df[col].idxmax()]. Let's take an example to understand it better.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable, col, to find the maximum value of that column.Find the maximum value and its corresponding row, using df.loc[df[col].idxmax()]Print the Step 4 output.Exampleimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, 7, 5, 1],       "z": [9, 3, 5, 1]    } ... Read More

How to get the correlation between two columns in Pandas?

Rishikesh Kumar Rishi
Updated on 12-Sep-2023 01:32:26

32K+ Views

We can use the .corr() method to get the correlation between two columns in Pandas. Let's take an example and see how to apply this method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize two variables, col1 and col2, and assign them the columns that you want to find the correlation of.Find the correlation between col1 and col2 by using df[col1].corr(df[col2]) and save the correlation value in a variable, corr.Print the correlation value, corr.Exampleimport pandas as pd df = pd.DataFrame(    {       "x": [5, 2, 7, 0],       "y": [4, ... Read More

How to filter rows in Pandas by regex?

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:51:18

18K+ Views

A regular expression (regex) is a sequence of characters that define a search pattern. To filter rows in Pandas by regex, we can use the str.match() method.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Initialize a variable regex for the expression. Supply a string value as regex, for example, the string 'J.*' will filter all the entries that start with the letter 'J'.Use df.column_name.str.match(regex) to filter all the entries in the given column name by the supplied regex.Example import pandas as pd df = pd.DataFrame(    dict(       name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], ... Read More

Python – Pandas Dataframe.rename()

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:40:06

4K+ Views

It's quite simple to rename a DataFrame column name in Pandas. All that you need to do is to use the rename() method and pass the column name that you want to change and the new column name. 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.Use rename() method to rename the column name. Here, we will rename the column "x" with its new name "new_x".Print the DataFrame with the renamed column.Example import pandas as pd df = pd.DataFrame(    {       "x": [5, 2, ... Read More

How to access a group of rows in a Pandas DataFrame?

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:35:39

3K+ Views

To access a group of rows in a Pandas DataFrame, we can use the loc() method. For example, if we use df.loc[2:5], then it will select all the rows from 2 to 5.StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.Print the input DataFrame, df.Use df.loc[2:5] to select the rows from 2 to 5.Print the DataFrame.Example import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0, 7, 0, 5, 2], "y": [4, 7, 5, 1, 5, 1, 4, 7], "z": [9, 3, 5, 1, 5, 1, 9, 3] } ) print "Input DataFrame is:", df df = df.loc[2:5] print "New DataFrame:", dfOutput Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3 New DataFrame: x y z 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1

Delete the first three rows of a DataFrame in Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 13:30:13

1K+ 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(    {       "x": [5, 2, 7, 0, 7, 0, 5, 2],       "y": [4, 7, 5, 1, 5, 1, 4, 7],       "z": [9, 3, 5, 1, 5, 1, 9, 3]    } ) print "Input DataFrame is:", df df = df.iloc[3:] print "After deleting the first 3 rows: ", dfOutput Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3 After deleting the first 3 rows: x y z 3 0 1 1 4 7 5 5 5 0 1 1 6 5 4 9 7 2 7 3

How to convert a DataFrame into a dictionary in Pandas?

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 df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) print "Input DataFrame is:", df print "Convert DataFrame into dictionary: ", df.to_dict()Output Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 Convert DataFrame into dictionary: {'x': {0: 5, 1: 2, 2: 7, 3: 0}, 'y': {0: 4, 1: 7, 2: 5, 3: 1}, 'z': {0: 9, 1: 3, 2: 5, 3: 1}}

Advertisements