본문 바로가기
Tech/Python

pandas - Data Import

by Jyubaeng2 2023. 7. 30.

Data Import with pandas

To import the Boston Housing Prices dataset and build a DataFrame with column names for the target and features, you can use the load_boston() function from sklearn.datasets and then create the DataFrame using pandas. Here's the code to do that:

import pandas as pd
import numpy as np
from sklearn.datasets import load_boston
boston = load_boston()
X_boston = boston.data
y_boston = boston.target

The dataset has data, target, feature_names attributes as shown below:

Boston House Prices dataset
Boston House Prices dataset description

In order to construct a dataframe, use the code below:

 

# Create a DataFrame for features
boston_df = pd.DataFrame(X_boston, columns=boston.feature_names)

# Add the target column to the DataFrame
boston_df['PRICE'] = y_boston

Boston House Prices dataset

 

https://ai-fin-tech.tistory.com/entry/Subsetting-Rows-with-Categorical-Variables

 

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

 

댓글