Found 26504 Articles for Server Side Programming

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

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

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

1K+ 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 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 "JSON output for input DataFrame: ", df.to_json("test.json") ... Read More

Python - How to Concatenate Two or More Pandas DataFrames along rows?

AmitDiwan
Updated on 14-Sep-2021 14:02:12

572 Views

To concatenate more than two Pandas DataFrames, use the concat() method. Set the axis parameter as axis = 0 to concatenate along rows. At first, import the required library −import pandas as pdLet us create the 1st DataFrame −dataFrame1 = pd.DataFrame(    {       "Col1": [10, 20, 30], "Col2": [40, 50, 60], "Col3": [70, 80, 90], }, index=[0, 1, 2], ) Let us create the 2nd DataFrame −dataFrame2 = pd.DataFrame(    {       "Col1": [100, 110, 120], "Col2": [130, 140, 150], "Col3": [160, 170, 180], }, ... Read More

How to save Pandas data into Excel multiple sheets?

Rishikesh Kumar Rishi
Updated on 04-Oct-2023 21:36:25

24K+ Views

To save Pandas DataFrames into multiple excel sheets, we can use the pd.ExcelWriter() method. Make sure you have the openpyxl package installed before using ExcelWriter().StepsCreate a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.Print the input DataFrame, df1.Create another DataFrame, df2, and print it.Use ExcelWriter() method to write the given formatted cells into an Excel sheet.Exampleimport pandas as pd df1 = pd.DataFrame( [[5, 2], [4, 1]], index=["One", "Two"], columns=["Rank", "Subjects"] ) df2 = pd.DataFrame( [[15, 21], [41, 11]], index=["One", "Two"], columns=["Rank", ... Read More

Select DataFrame rows between two index values in Python Pandas

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:57:27

6K+ 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 df[index_lower_limit: index_upper_limit] to print the DataFrame in range index.Exampleimport pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, ... Read More

Selecting with complex criteria from a Pandas DataFrame

Rishikesh Kumar Rishi
Updated on 14-Sep-2021 12:40:37

179 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 condition holds, else False. 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 col, with a column name.Perform some comparison operations.Print the resultant DataFrame.Example import pandas as pd df = pd.DataFrame( ... Read More

Python - How to Concatenate Two or More Pandas DataFrames along columns?

AmitDiwan
Updated on 14-Sep-2021 13:56:48

515 Views

To concatenate more than two Pandas DataFrames, use the concat() method. Set the axis parameter as axis = 1 to concatenate along columns. At first, import the required library −import pandas as pdLet us create the 1st DataFrame −dataFrame1 = pd.DataFrame(    { "Col1": [10, 20, 30], "Col2": [40, 50, 60], "Col3": [70, 80, 90],    }, index=[0, 1, 2], )Let us create the 2nd DataFrame −dataFrame2 = pd.DataFrame(    {       "Col1": [100, 110, 120], "Col2": [130, 140, 150], "Col3": [160, 170, 180], }, ... Read More

Advertisements