In [1]:
# Dropping a Single Column
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6],
''''C'''': [7, 8, 9]
})
df = df.drop(''''B'''', axis=1)
print(df)
A C 0 1 7 1 2 8 2 3 9
In [2]:
# Dropping Multiple Columns
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6],
''''C'''': [7, 8, 9]
})
df = df.drop([''''B'''', ''''C''''], axis=1)
print(df)
A 0 1 1 2 2 3
In [3]:
# Dropping a Column Using del
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6],
''''C'''': [7, 8, 9]
})
del df[''''B'''']
print(df)
A C 0 1 7 1 2 8 2 3 9
In [4]:
# Dropping a Column by Index
import pandas as pd
df = pd.DataFrame({
''''A'''': [1, 2, 3],
''''B'''': [4, 5, 6],
''''C'''': [7, 8, 9]
})
df = df.drop(df.columns[1], axis=1)
print(df)
A C 0 1 7 1 2 8 2 3 9