溫馨提示×

python怎么刪除表中的一列數(shù)據(jù)

小億
127
2024-01-25 12:20:20
欄目: 編程語言

要?jiǎng)h除表中的一列數(shù)據(jù),可以使用pandas庫中的drop()函數(shù)。下面是一個(gè)示例代碼:

import pandas as pd

# 創(chuàng)建一個(gè)DataFrame示例
data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}
df = pd.DataFrame(data)

# 刪除列B
df = df.drop('B', axis=1)

print(df)

輸出結(jié)果將是:

   A  C
0  1  7
1  2  8
2  3  9

在這個(gè)示例中,使用drop()函數(shù)刪除了列B,參數(shù)axis=1表示刪除列,axis=0表示刪除行。

0