Home >Backend Development >Python Tutorial >In-depth understanding of the techniques for deleting row data in pandas
Data processing skills: Detailed explanation of how to delete rows in pandas
In data processing, it is often necessary to delete certain rows of data in the DataFrame. Pandas is a powerful data processing library that provides a variety of methods to implement row data deletion operations. This article will introduce in detail several common methods of deleting rows in pandas and provide specific code examples.
import pandas as pd data = {'Name': ['Tom', 'Nick', 'John', 'David'], 'Age': [20, 25, 30, 35], 'Gender': ['M', 'M', 'M', 'M']} df = pd.DataFrame(data) # 删除索引为2的行数据 df = df.drop(2) print(df)
The output result is as follows:
Name Age Gender 0 Tom 20 M 1 Nick 25 M 3 David 35 M
As you can see, the drop method returns a new DataFrame and deletes the specified rows in the result. .
import pandas as pd data = {'Name': ['Tom', 'Nick', 'John', 'David'], 'Age': [20, 25, 30, 35], 'Gender': ['M', 'M', 'M', 'M']} df = pd.DataFrame(data) # 删除所有年龄小于30的行数据 df = df[df['Age'] >= 30] print(df)
The output is as follows:
Name Age Gender 2 John 30 M 3 David 35 M
As you can see, by setting the Boolean index to True or False, we can filter out the row data that needs to be retained.
import pandas as pd data = {'Name': ['Tom', 'Nick', 'John', 'David'], 'Age': [20, 25, 30, 35], 'Gender': ['M', 'M', 'M', 'M']} df = pd.DataFrame(data) # 删除索引为1到2的行数据 df = df.drop(df.index[1:3]) print(df)
The output is as follows:
Name Age Gender 0 Tom 20 M 3 David 35 M
As you can see, by setting the index range of the slicing operation, we can delete consecutive multiple rows of data.
import pandas as pd data = {'Name': ['Tom', 'Nick', 'John', 'David'], 'Age': [20, 25, 30, 35], 'Gender': ['M', 'M', 'M', 'M']} df = pd.DataFrame(data) # 设置第三行的索引为缺失 df.set_index(pd.Index(['0', '1', '3']), inplace=True) # 重置索引并删除缺失的行 df.reset_index(drop=True, inplace=True) print(df)
The output is as follows:
Name Age Gender 0 Tom 20 M 1 Nick 25 M 2 David 35 M
You can see that by setting the index to the missing row, and using the reset_index method to reset the index and delete the missing row , we can implement the operation of deleting specific rows.
To sum up, here are several common methods to delete row data in pandas DataFrame. According to different needs, we can choose a suitable method to complete the data processing task. In practical applications, appropriate methods can be selected to delete row data according to specific circumstances to improve the efficiency and accuracy of data processing.
The above is the detailed content of In-depth understanding of the techniques for deleting row data in pandas. For more information, please follow other related articles on the PHP Chinese website!