[Python] 11. Deleting

hhyun·2024년 6월 23일

[Python]

목록 보기
11/11

📖 Deleting

Data Read

import pandas as pd
import numpy as np

df = pd.read_csv('링크')
df.head()

💭 Q126. Column명을 아래와 같이 변경

# 1. sepal_length
# 2. sepal_width
# 3. petal_length
# 4. petal_width
# 5. class
df.columns = ['sepal_length','sepal_width', 'petal_length', 'petal_width', 'class']
df.head()

💭 Q127. Column별 NA값이 존재하는지 확인

df.isnull().sum()
# nice no missing value

💭 Q127. 'petal_length'의 10번째부터 29번째 행의 값을 NaN으로 변환

df.iloc[9:29,2] = np.nan
df.head(30)

💭 Q128. Nan값을 다시 1로 채우기

df.petal_length.fillna(1, inplace = True)
print(df.petal_length.isnull().sum())

💭 Q129.Class 컬럼을 삭제

# df = df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
del df['class']
df.head()

💭 Q130.3개의 행에 대해서 모두 Nan값으로 변경

  • np.nan : Nan값으로 변경
df.iloc[0:3 ,:] = np.nan
df.head()

💭 Q131.NaN을 가지고 있는 행을 삭제

  • dropna() : 결측값이 포함된 레이블을 제거
df = df.dropna(how='any')
df.head()

💭 Q132.index를 다시 0부터 시작할 수 있도록 수정

df = df.reset_index(drop = True)
df.head()

0개의 댓글