Home >Backend Development >Python Tutorial >Tutorial on deleting row data using pandas

Tutorial on deleting row data using pandas

WBOY
WBOYOriginal
2024-01-09 14:33:534779browse

Tutorial on deleting row data using pandas

pandas tutorial: How to use pandas to delete row data, specific code examples are required

Introduction:
In data analysis and processing, data often needs to be cleaned And processing, deleting unnecessary or invalid rows of data in a data set is a common operation. In Python, the pandas library provides powerful data manipulation tools. This article will introduce how to use pandas to delete row data and give specific code examples.

  1. Import pandas library
    Before starting, you first need to import the pandas library.

import pandas as pd

  1. Create sample data
    For demonstration, we first create a sample data containing some rows of data. The following code creates a DataFrame object named "data" and adds some rows of data.

data = {'Name': ['Zhang San', 'Li Si', 'Wang Wu', 'Zhao Liu', 'Liu Qi'],

    '年龄': [20, 25, 30, 35, 40],
    '性别': ['男', '男', '女', '男', '女']}

df = pd.DataFrame(data)

print("Original data:")
print(df)

Output result:
Original data:
Name Age Gender
0 Zhang San 20 Male
1 Li Si 25 Male
2 Wang Wu 30 Female
3 Zhao Liu 35 Male
4 Liu Qi 40 Female

  1. Use Conditional deletion of row data
    pandas provides a variety of methods to delete row data. The most common way is to delete rows through conditions, that is, only delete row data that meets certain conditions. The following sample code demonstrates how to delete rows with an age greater than or equal to 30.

df = df[df['age']

print("Delete data with age greater than or equal to 30:")
print(df)

Output result:
Delete data whose age is greater than or equal to 30:
Name Age Gender
0 Zhang San 20 Male
1 Li Si 25 Male

  1. Use index to delete row data
    In addition to using conditions to delete, you can also use index to delete rows in the data set. By specifying the index label of the row, pandas can easily delete the specified row of data. The following sample code demonstrates how to delete the first and last rows of data.

df = df.drop([0, 4])

print("Delete the first and last rows of data:")
print(df)

Output results:
Delete the first and last rows of data:
Name, Age, Gender
1 Li Si 25 Male
2 Wang Wu 30 Female
3 Zhao Liu 35 Male

  1. Use row numbers to delete row data
    In addition to using index tags, you can also use row numbers to delete row data. Pandas provides the "iloc" method, which can delete specified row data by setting the row number. The following sample code demonstrates how to delete the second and third rows of data.

df = df.drop(df.index[[1, 2]])

print("Delete the data in the second and third rows:")
print(df)

Output result:
Delete the second and third rows of data:
Name Age Gender
0 Zhang San 20 Male
3 Zhao Liu 35 Male

  1. Modify the original data
    In the above example, deleting row data is performed on a copy of the DataFrame object, and the original data has not changed. If you want to modify the original data, you need to add an additional parameter "inplace=True". The following sample code demonstrates how to delete rows that meet a condition directly on the original data.

df.drop(df[df['age'] >= 30].index, inplace=True)

print("Directly delete the age greater than Data equal to 30: ")
print(df)

Output result:
Delete data whose age is greater than or equal to 30 directly on the original data:
Name Age Gender
0 pieces Three 20 Male
1 Li Si 25 Male

Conclusion:
By using the pandas library and the above code example, we can easily delete row data in the DataFrame object. Through conditions, index labels or row numbers, we can selectively delete rows of data that meet specific conditions. This provides us with very convenient tools and methods for data cleaning and processing.

The above is the detailed content of Tutorial on deleting row data using pandas. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn