딕셔너리를 활용해서 변수 생성
2022. 4. 5. 20:50ㆍ파이썬/파이썬 일반 꿀팁
import pandas as pd
df = pd.DataFrame({'col1': [1, 2, 2, 3, 1],
'col2': ['negative', 'positive', 'neutral', 'neutral', 'positive']})
conversion_dict = {'negative': -1, 'neutral': 0, 'positive': 1}
df['converted_column'] = df['col2'].replace(conversion_dict)
print(df.head())
결과)
col1 col2 converted_column
0 1 negative -1
1 2 positive 1
2 2 neutral 0
3 3 neutral 0
4 1 positive 1
'파이썬 > 파이썬 일반 꿀팁' 카테고리의 다른 글
더미변수 다시 원래대로 돌리기 (0) | 2022.07.11 |
---|---|
리스트 순서 유지하면서 중복 제거 방법 (0) | 2022.05.12 |
itertools (순열, 조합) (0) | 2021.06.30 |
반복문에서 zip 활용 (0) | 2021.06.01 |
중복되는 데이터 확인 (set) (0) | 2021.06.01 |