![]() |
to manipulate and analyze data efficiently What is Python Pandas?A Python library called Pandas was created to analyze and manipulate a wide variety of data, including time series, tabular data, and many kinds of data sets. Data sets in a variety of formats, including relational database tables, Excel files, XML files, comma-separated values (CSV) files, and JavaScript object notation (JSON) files, can be processed by pandas. Pandas was developed by Wes McKinney in 2008, and it was made available as an open-source project in 2010 so that anybody may contribute to its advancement. Using NumPy, a different Python library that provides features like n-dimensional arrays, McKinney built Pandas. Uses of Python PandasBelow, are the uses of Pandas Library.
How to Use Python Pandas?Using Python Pandas requires multiple steps to manipulate and analyze data efficiently. Here’s a simple guide for using Pandas: Install Pandas LibraryBefore using the pandas in our code we need to install it in our system, for install the pandas library use the below command. pip install pandas Import Pandas Library to PythonIf we want to use the pandas library’s functions, we first need to import it into Python. We can achieve that using the Python syntax shown below: import pandas as pd Create DataFrame with Pandas Library in PythonThe pandas library’s ability to generate new DataFrame objects is a very important feature. For this, we can use the pd.DataFrame() function, as seen below:
Output Name Age Gender 0 Sangita 25 Female 1 Rohan 30 Male 2 Max 35 Male Python Pandas ExamplesBelow are some of the examples by which we can understand how we can use Python Pandas to create and insert row and column in the DataFrame in Python: Example 1: Add New Column to Pandas DataFrameIn this example, we import the Pandas library and create a DataFrame from dictionary data with columns for ‘Name‘, ‘Age‘, and ‘Gender‘. To add a new ‘Location‘ column, assign a list of values to df[‘Location’], ensuring its length matches the DataFrame’s rows. Finally, we print the DataFrame to observe the new ‘Location’ column.
Output Name Age Gender Location 0 Rahul 25 Male Delhi 1 Mahi 30 Female Banglore 2 Ram 35 Male Noida Example 2: Remove Column From Pandas DataFrameIn this example, we create a DataFrame df from dictionary data containing columns for ‘Name‘, ‘Age‘, and ‘Gender‘. To remove the ‘Gender‘ column, we use the drop() function with the columns parameter set to ‘Gender‘ and inplace=True to modify the DataFrame in place. Finally, we print the DataFrame to observe the changes after removing the ‘Gender‘ column.
Output Name Age 0 Rahul 25 1 Riya 30 2 Rohit 35 Example 3: Add New Row to Pandas DataFrameIn this example, I’ll demonstrate adding a new row to the bottom of a DataFrame. We begin by creating a DataFrame df with columns for ‘Name’, ‘Age’, and ‘Gender’ using dictionary data. To add a new row, we define the data in a dictionary called new_row. We utilize the pd.concat() method to append the new entry to the DataFrame, specifying ignore_index=True to reindex the DataFrame after adding the new row.
Output Name Age Gender 0 Rahul 25 Male 1 Raksha 30 Female 2 Mohit 35 Male 3 Sakshi 28 Female Example 4: Remove Row from Pandas DataFrameIn this example demonstrates how to delete a row from a Pandas DataFrame in Python. We start by creating a DataFrame df with columns for ‘Name’, ‘Age’, and ‘Gender’ using dictionary data. To remove a row based on a condition, we utilize boolean indexing. In this case, we use df[‘Name’] != ‘Alice’ to select all rows where the ‘Name’ column is not ‘Alice’. This effectively removes the entry with the name ‘Mohit’ from the DataFrame.
Output Name Age Gender 1 Sonal 30 Female 2 Rishav 35 Male |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |