pandas—delete a row or column of data

First, create a DataFrame format data as example data.

# 创建一个DataFrame格式数据
data = {
    
    'a': ['a0', 'a1', 'a2'],
        'b': ['b0', 'b1', 'b2'],
        'c': [i for i in range(3)],
        'd': 4}
df = pd.DataFrame(data)
print('举例数据情况:\n', df)

Insert image description here
Note: DataFrame is the most commonly used pandas object. After using pandas to read the data file, the data is stored in memory in the DataFrame data structure.

To delete rows and columns of pandas data, the drop() and del functions are mainly used. The usage is as follows:
1. drop() function
Syntax:
DataFrame.drop(labels,axis=0,level=None,inplace=False,errors='raise')

parameter illustrate
labels Receives a string or array representing the label (row name or column name) of the row or column to be deleted. no default value
axis Receives 0 or 1, representing the axis (row or column) of the operation. The default is 0, which represents rows; 1, which represents columns.
level Receives an int or index name, representing the level of the label. Default is None
inplace Receives a Boolean value, indicating whether the operation is effective on the original data. The default is False.
errors errors='raise' will cause the program to throw an error when labels receive no row names or column names, causing the program to stop running. errors='ignore' will ignore no row names or column names, and only treat existing row names or column names. Perform operations on column names. The default is 'errors='raise''.

Example 1: Delete column d

df1 = df.drop(labels='d', axis=1)
print('删除d列前:\n', df)
print('删除d列后:\n', df1)

Insert image description here
Example 2: Delete the first row

df2 = df.drop(labels=0)
print('删除前:\n', df)
print('删除列:\n', df2)

Insert image description here
Example 3: Delete multiple rows and columns at the same time

df3 = df.drop(labels=['a', 'b'], axis=1) # 同时删除a,b列
df4 = df.drop(labels=range(2)) # 等价于df.drop(labels=[0,1])
print('删除前:\n', df)
print('删除多列(a,b):\n', df3)
print('删除多行(第1,2行):\n', df4)

Insert image description here
Note: (1) When deleting a column, the axis parameter cannot be omitted, because the axis defaults to 0 (row); (
2) If the inplace parameter is not added, the original data will not be modified by default, and the result needs to be assigned to the new Variables.

2. Del function
syntax: del df['column name']
This operation will delete the original data df, and only one column can be deleted at a time.
Correct usage:

del df['d']
print('原地删除d列后:\n', df)

Insert image description here
Incorrect usage:

del df[['a', 'b']]
print(df)

Insert image description here
The above is the usage of pandas to delete data in a certain row and column. Compared with del(), drop() is more flexible and more practical.

—end—
[Search [one-digit code] on WeChat to follow me]

Guess you like

Origin blog.csdn.net/LHJCSDNYL/article/details/124784943