Pandas를 사용하여 데이터프레임의 열 데이터 유형 변환
여기에는 Pandas를 사용하여 데이터프레임의 열 데이터 유형을 변환하는 Python 코드가 포함되어 있으며, 결과는 해당 데이터프레임의 열 데이터 유형이 변환된 것을 보여줍니다.
import pandas as pd
# 샘플 데이터프레임 생성
data = {'A': ['1', '2', '3'],
'B': [4.1, 5.2, 6.3],
'C': [True, False, True]}
df = pd.DataFrame(data)
# 데이터 타입이 있는 원본 데이터프레임 출력
print("원본 데이터프레임:")
print(df.dtypes)
# 'A' 열을 정수로 변환
df['A'] = df['A'].astype(int)
# 'B' 열을 부동 소수점으로 변환
df['B'] = df['B'].astype(float)
# 'C' 열을 부울로 변환
df['C'] = df['C'].astype(bool)
# 데이터 타입 변환 후의 데이터프레임 출력
print("\n데이터 타입 변환 후의 데이터프레임:")
print(df.dtypes)
코드 실행 결과
원본 데이터프레임:
A object
B float64
C bool
dtype: object
데이터 타입 변환 후의 데이터프레임:
A int64
B float64
C bool
dtype: object
보시다시피, 'A' 열, 'B' 열 및 'C' 열의 데이터 유형이 각각 int64, float64 및 bool로 성공적으로 변환되었습니다.
dplyr - Change Data Type for Column
dplyr - Change Data Type for Column
R에서는 데이터프레임을 다루는 주요 패키지 중 하나로 'dplyr' 패키지를 사용할 수 있습니다. 아래 코드는 'dplyr' 패키지를 사용하여 데이터프레임의 열 데이터 유형을 변환합니다. 결과는 데이터
ai-fin-tech.tistory.com
pandas - Subsetting Rows with Categorical Variables
pandas - Subsetting Rows with Categorical Variables
Data Import Since there is no categorical variables in Boston dataset, I will just show you the example using dummy dataset. Let's consider a hypothetical dataset called "employee_data" with a categorical variable "Department" and other numerical features.
ai-fin-tech.tistory.com
pandas - Basic DataFrame Inspection
pandas - Basic DataFrame Inspection
Data Import import pandas as pd from sklearn.datasets import load_boston # Load the Boston Housing Prices dataset boston = load_boston() boston_df = pd.DataFrame(boston.data, columns=boston.feature_names) boston_df['PRICE'] = boston.target Inspect DataFram
ai-fin-tech.tistory.com
'Tech > Python' 카테고리의 다른 글
Data Handling - Numerical Data (0) | 2023.08.18 |
---|---|
pandas - Joining Datasets (0) | 2023.08.16 |
Data Handling - Dates and Times (38) | 2023.08.04 |
Data Handling - fit, transform, and fit_transform (8) | 2023.08.03 |
Data Handling - Data Type (2) | 2023.08.03 |
댓글