日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

DataFrame 学习笔记

發(fā)布時(shí)間:2025/4/5 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataFrame 学习笔记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

由數(shù)組字典構(gòu)造DataFrame

from pandas import Series,DataFrame import numpy as np import pandas as pd data={'state':['ohio','ohio','ohio','Nevada','Nevada'],'year':[2000,2001,2002,2001,2002],'pop':[1.5,1.7,3.6,2.4,2.9]} frame=DataFrame(data) Out[103]: pop state year 0 1.5 ohio 2000 1 1.7 ohio 2001 2 3.6 ohio 2002 3 2.4 Nevada 2001 4 2.9 Nevada 2002

改變序列順序

DataFrame(data,columns=['year','state','pop'])Out[104]: year state pop 0 2000 ohio 1.5 1 2001 ohio 1.7 2 2002 ohio 3.6 3 2001 Nevada 2.4 4 2002 Nevada 2.9

改變標(biāo)簽,增加列沒(méi)有數(shù)據(jù),顯示NaN

frame2=DataFrame(data,columns=['year','state','pop','debt'], index=['one','two','three','four','five'])frame2 Out[108]: year state pop debt one 2000 ohio 1.5 NaN two 2001 ohio 1.7 NaN three 2002 ohio 3.6 NaN four 2001 Nevada 2.4 NaN five 2002 Nevada 2.9 NaN frame2.columns Out[111]: Index(['year', 'state', 'pop', 'debt'], dtype='object')

frame2.state=frame2[‘state’] 用法相同

frame2.state Out[112]: one ohio two ohio three ohio four Nevada five Nevada Name: state, dtype: objectframe2['state'] Out[113]: one ohio two ohio three ohio four Nevada five Nevada Name: state, dtype: object

debt賦值

frame2['debt']=16.5frame2 Out[115]: year state pop debt one 2000 ohio 1.5 16.5 two 2001 ohio 1.7 16.5 three 2002 ohio 3.6 16.5 four 2001 Nevada 2.4 16.5 five 2002 Nevada 2.9 16.5 frame2['debt']=np.arange(5) frame2.debt=np.arange(5) frame2 Out[120]: year state pop debt one 2000 ohio 1.5 0 two 2001 ohio 1.7 1 three 2002 ohio 3.6 2 four 2001 Nevada 2.4 3 five 2002 Nevada 2.9 4 val=Series([-1.2,-1.5,-1.7],index=['two','four','five'])frame2['debt']=valframe2 Out[127]: year state pop debt one 2000 ohio 1.5 NaN two 2001 ohio 1.7 -1.2 three 2002 ohio 3.6 NaN four 2001 Nevada 2.4 -1.5 five 2002 Nevada 2.9 -1.7 frame2['eastern']=frame2.state=='ohio'frame2 Out[129]: year state pop debt eastern one 2000 ohio 1.5 NaN True two 2001 ohio 1.7 -1.2 True three 2002 ohio 3.6 NaN True four 2001 Nevada 2.4 -1.5 False five 2002 Nevada 2.9 -1.7 False 刪除列 del frame2['eastern']frame2 Out[131]: year state pop debt one 2000 ohio 1.5 NaN two 2001 ohio 1.7 -1.2 three 2002 ohio 3.6 NaN four 2001 Nevada 2.4 -1.5 five 2002 Nevada 2.9 -1.7frame2.columns Out[132]: Index(['year', 'state', 'pop', 'debt'], dtype='object')

總結(jié)

以上是生活随笔為你收集整理的DataFrame 学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。