pd.dataframe和series以及np.narray的维度升降
生活随笔
收集整理的這篇文章主要介紹了
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 進行操作。這是一種降維操作。
總結
以上是生活随笔為你收集整理的pd.dataframe和series以及np.narray的维度升降的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ArcGIS Server WMTS服务
- 下一篇: google建立多人同时编辑的文档或者表