파이썬/데이터프레임
python(vscode)/데이터 프레임/두개 변수 사용시 연동됨.
gongdol
2024. 3. 31. 19:08
300x250
1. 문제상황
- 데이터 프레임 정의후 임시로 다른변수 (temp)에 넣고 temp에서 데이터 추가했는데, df 도 변경됨.
import pandas as pd
df = pd.DataFrame()
df['Test'] = [1, 2, 3]
temp = pd.DataFrame()
temp = df
temp['New Column'] = [2, 3, 4]
print(df)
2. 문제해결
- 데이터 프레임 카피해줘야한다. 그냥 넣으면 안됨
import pandas as pd
df = pd.DataFrame()
df['Test'] = [1, 2, 3]
temp = pd.DataFrame()
temp = df.copy()
temp['New Column'] = [2, 3, 4]
print(df)
300x250