pd.dataframe和series以及np.narray的維度升降


 

1.第一步讀入泰坦尼克號數據集

import pandas as pd
data = pd.read_csv(r".\Narrativedata.csv"
,index_col=0
)#index_col=0將第0列作為索引,不寫則認為第0列為特征

data.head()

 

 

2.通過df的loc的函數從df中取出一列的數據,該數據格式為  : <class 'pandas.core.series.Series'>

age1=data.loc[:,"Age"]
print(type(age1))
age1.head()

 

 會發現數據維度:(891,)

 

 

 

3.數據維度的轉換 升維

因為sklearn里面的數據必須是二維

   1.使用Series類的 to_dataframe

print(type(Age.to_frame()))
print(Age.to_frame().shape)
Age.to_frame().head()

 

  2.使用reshape和values.reshape

age1=Age.reshape(-1,1)

 

 

 age2=Age.values.reshape(-1,1)

 

 

 

print(type(age2))
age2.shape

 

3.數據的降維

DataFrame 有個 apply 方法,就是把函數映射到 DataFrame 里面每個 Series 上,對 Series 進行操作。這是一種降維操作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM