1 개요[ | ]
- Pandas 데이터프레임 컬럼명으로 컬럼 제거
Python
CPU
3.3s
MEM
95M
4.0s
Reload
Copy
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
Copy
df.drop(columns=['sepal_length','petal_length'])
sepal_width | petal_width | species | |
---|---|---|---|
0 | 3.5 | 0.2 | setosa |
1 | 3.0 | 0.2 | setosa |
2 | 3.2 | 0.2 | setosa |
3 | 3.1 | 0.2 | setosa |
4 | 3.6 | 0.2 | setosa |
... | ... | ... | ... |
145 | 3.0 | 2.3 | virginica |
146 | 2.5 | 1.9 | virginica |
147 | 3.0 | 2.0 | virginica |
148 | 3.4 | 2.3 | virginica |
149 | 3.0 | 1.8 | virginica |
150 rows × 3 columns
2 방법 1: drop()[ | ]
inplace
Copy
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df.drop(columns=['sepal_length','petal_length'], inplace=True)
df.head()
sepal_width | petal_width | species | |
---|---|---|---|
0 | 3.5 | 0.2 | setosa |
1 | 3.0 | 0.2 | setosa |
2 | 3.2 | 0.2 | setosa |
3 | 3.1 | 0.2 | setosa |
4 | 3.6 | 0.2 | setosa |
not-inplace
Copy
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df2 = df.drop(columns=['sepal_length','petal_length'])
df2.head()
sepal_width | petal_width | species | |
---|---|---|---|
0 | 3.5 | 0.2 | setosa |
1 | 3.0 | 0.2 | setosa |
2 | 3.2 | 0.2 | setosa |
3 | 3.1 | 0.2 | setosa |
4 | 3.6 | 0.2 | setosa |
3 방법 2: del[ | ]
inplace
Copy
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
del df['sepal_length']
del df['petal_length']
df.head()
sepal_width | petal_width | species | |
---|---|---|---|
0 | 3.5 | 0.2 | setosa |
1 | 3.0 | 0.2 | setosa |
2 | 3.2 | 0.2 | setosa |
3 | 3.1 | 0.2 | setosa |
4 | 3.6 | 0.2 | setosa |
not-inplace
Copy
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df2 = df.copy()
del df2['sepal_length']
del df2['petal_length']
(df.columns, df2.columns)
(Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'], dtype='object'), Index(['sepal_width', 'petal_width', 'species'], dtype='object'))
4 같이 보기[ | ]
- 파이썬 del
- Pandas .drop()
- Pandas 데이터프레임 컬럼 제거
- Pandas 데이터프레임 인덱스로 컬럼 제거
- Pandas 데이터프레임 컬럼명으로 컬럼 선택
- R 컬럼명으로 컬럼 제거
5 참고[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.