Home > Article > Backend Development > How to delete rows in pandas
Pandas methods for deleting rows include using the drop() function, using the index to delete rows, using conditions to delete rows, and using the iloc() function to delete rows. Detailed introduction: 1. Use the drop() function: Pandas provides a drop() function that can delete rows by specifying an index or label. The syntax of this function is "DataFrame.drop(labels, axis=0, inplace=False)"; 2. Use the index to delete rows: you can directly use the index to delete, etc.
The operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.
Pandas is a powerful Python library for data analysis and data manipulation. When dealing with large amounts of data, sometimes you need to delete certain rows in a DataFrame. This article will introduce several ways to delete rows using Pandas.
Method 1: Use the drop() function
Pandas provides a drop() function that can delete rows by specifying an index or label. The syntax of this function is as follows:
DataFrame.drop(labels, axis=0, inplace=False)
Among them, labels represents the index or label of the row to be deleted, axis represents the direction of deletion, 0 represents the row, and 1 represents the column. When inplace is False, the original DataFrame will not be modified; when it is True, the original DataFrame will be modified.
Sample code:
import pandas as pd # 创建一个示例DataFrame data = {'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd']} df = pd.DataFrame(data) # 删除第一行 df = df.drop(0) print(df) ``` 输出结果: ``` A B 1 2 b 2 3 c 3 4 d
Method 2: Use index to delete rows
If we know the index of the row to be deleted, we can directly use the index to delete.
Sample code:
import pandas as pd # 创建一个示例DataFrame data = {'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd']} df = pd.DataFrame(data) # 删除第一行 df = df.drop(df.index[0]) print(df) ``` 输出结果: ``` A B 1 2 b 2 3 c 3 4 d
Method 3: Use conditions to delete rows
Sometimes, we need to delete rows based on a certain condition. This can be achieved using Boolean indexing.
Sample code:
import pandas as pd # 创建一个示例DataFrame data = {'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd']} df = pd.DataFrame(data) # 根据条件删除行 df = df[df['A'] != 2] print(df)
Output result:
A B 0 1 a 2 3 c 3 4 d
Method 4: Use the iloc() function to delete rows
Pandas provides an iloc() function , used to delete rows based on position.
Sample code:
import pandas as pd # 创建一个示例DataFrame data = {'A': [1, 2, 3, 4], 'B': ['a', 'b', 'c', 'd']} df = pd.DataFrame(data) # 删除第一行 df = df.drop(df.index[[0]]) print(df)
Output results:
A B 1 2 b 2 3 c 3 4 d
Summary:
This article introduces several ways to delete rows using Pandas, including using drop( ) function, delete rows using index, delete rows using condition, and delete rows using iloc() function. Choose the appropriate method to delete rows in the DataFrame according to actual needs, which helps to process large amounts of data more efficiently
The above is the detailed content of How to delete rows in pandas. For more information, please follow other related articles on the PHP Chinese website!