Delete Columns in a DataFrame
Resource
Data Source
Import Pandas
# Import Pandas
import pandas as pd
Set File Path and Read it into a DataFrame
# Set file path
fp = "data/cereal.csv"
# Read file to DataFrame
df = pd.read_csv(fp, delimiter=',')
Example One
# Example One
# Delete All Columns Except Named Columns
df_1 = df[['name', 'rating']]
df_1
name | rating | |
---|---|---|
0 | 100% Bran | 68.402973 |
1 | 100% Natural Bran | 33.983679 |
2 | All-Bran | 59.425505 |
3 | All-Bran with Extra Fiber | 93.704912 |
4 | Almond Delight | 34.384843 |
... | ... | ... |
72 | Triples | 39.106174 |
73 | Trix | 27.753301 |
74 | Wheat Chex | 49.787445 |
75 | Wheaties | 51.592193 |
76 | Wheaties Honey Gold | 36.187559 |
77 rows × 2 columns
Example Two
# Example Two
# Use the pandas.DataFrame.loc method | https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html
df_2 = df.loc[:, ['name', 'rating']]
df_2
name | rating | |
---|---|---|
0 | 100% Bran | 68.402973 |
1 | 100% Natural Bran | 33.983679 |
2 | All-Bran | 59.425505 |
3 | All-Bran with Extra Fiber | 93.704912 |
4 | Almond Delight | 34.384843 |
... | ... | ... |
72 | Triples | 39.106174 |
73 | Trix | 27.753301 |
74 | Wheat Chex | 49.787445 |
75 | Wheaties | 51.592193 |
76 | Wheaties Honey Gold | 36.187559 |
77 rows × 2 columns
Example 3
# Example 3
# Use the drop method # https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html
df_3 = df.drop(columns=['name', 'rating'])
df_3
mfr | type | calories | protein | fat | sodium | fiber | carbo | sugars | potass | vitamins | shelf | weight | cups | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | N | C | 70 | 4 | 1 | 130 | 10.0 | 5.0 | 6 | 280 | 25 | 3 | 1.0 | 0.33 |
1 | Q | C | 120 | 3 | 5 | 15 | 2.0 | 8.0 | 8 | 135 | 0 | 3 | 1.0 | 1.00 |
2 | K | C | 70 | 4 | 1 | 260 | 9.0 | 7.0 | 5 | 320 | 25 | 3 | 1.0 | 0.33 |
3 | K | C | 50 | 4 | 0 | 140 | 14.0 | 8.0 | 0 | 330 | 25 | 3 | 1.0 | 0.50 |
4 | R | C | 110 | 2 | 2 | 200 | 1.0 | 14.0 | 8 | -1 | 25 | 3 | 1.0 | 0.75 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
72 | G | C | 110 | 2 | 1 | 250 | 0.0 | 21.0 | 3 | 60 | 25 | 3 | 1.0 | 0.75 |
73 | G | C | 110 | 1 | 1 | 140 | 0.0 | 13.0 | 12 | 25 | 25 | 2 | 1.0 | 1.00 |
74 | R | C | 100 | 3 | 1 | 230 | 3.0 | 17.0 | 3 | 115 | 25 | 1 | 1.0 | 0.67 |
75 | G | C | 100 | 3 | 1 | 200 | 3.0 | 17.0 | 3 | 110 | 25 | 1 | 1.0 | 1.00 |
76 | G | C | 110 | 2 | 1 | 200 | 1.0 | 16.0 | 8 | 60 | 25 | 1 | 1.0 | 0.75 |
77 rows × 14 columns