Notice
Recent Posts
Recent Comments
Link
아미(아름다운미소)
pandas filter 본문
import pandas as pd
# 데이터프레임 생성
df = pd.DataFrame({'a': [1, 2, None, 4, None], 'b': ['apple', 'banana', 'cherry', 'date', 'elderberry'], 'd': [None, None, None, None, None]})
# a 컬럼이 null인 경우 b 컬럼을 확인하여 모든 행을 뽑아내고 알파벳순으로 정렬한 후 첫 번째 값을 a 컬럼에 재할당
df.loc[df['a'].isnull(), 'a'] = df.loc[df['a'].isnull(), 'b'].sort_values().iloc[0]
# a 컬럼이 null인 경우 d 컬럼에 1을 채워넣기
df.loc[df['a'].isnull(), 'd'] = 1
print(df)
import pandas as pd
# 데이터프레임 생성
df = pd.DataFrame({'a': [1, 2, None, 4, None], 'b': ['apple', 'banana', 'cherry', 'date', 'elderberry']})
# a 컬럼이 null인 경우 b 컬럼을 확인하여 모든 행을 뽑아내고 알파벳순으로 정렬한 후 첫 번째 값을 a 컬럼에 재할당
df.loc[df['a'].isnull(), 'a'] = df.loc[df['a'].isnull(), 'b'].sort_values().iloc[0]
print(df)
import pandas as pd
df = pd.DataFrame({'a': [1, 2, None, 4, None], 'b': ['apple', 'banana', 'cherry', 'date', 'elderberry']})
# a 컬럼이 null인 경우 b 컬럼을 확인하여 모든 행을 뽑아내고 알파벳순으로 정렬한 후 첫 번째 값을 a 컬럼에 재할당
df.loc[df['a'].isnull(), 'a'] = df.loc[df['a'].isnull(), 'b'].sort_values().iloc[0]
print(df)
'랭귀지 > pandas' 카테고리의 다른 글
pandas 참조 df 특정 컬럼값으로 값채우기 (0) | 2024.07.19 |
---|---|
pandas dataframe 컬럼비교 (0) | 2024.07.18 |
pandas 여러열적용 (0) | 2024.07.16 |
pandas 조건 (0) | 2024.07.16 |
pandas 빈데이타프레임처리 (0) | 2024.07.15 |
Comments