柚子快報(bào)激活碼778899分享:Pandas初體驗(yàn)
柚子快報(bào)激活碼778899分享:Pandas初體驗(yàn)
文章目錄
第1關(guān):了解數(shù)據(jù)處理對(duì)象--Series第2關(guān):了解數(shù)據(jù)處理對(duì)象-DataFrame第3關(guān):讀取CSV格式數(shù)據(jù)第4關(guān):數(shù)據(jù)的基本操作——?jiǎng)h除
第1關(guān):了解數(shù)據(jù)處理對(duì)象–Series
本關(guān)任務(wù) 創(chuàng)建一個(gè)名為series_a的series數(shù)組,當(dāng)中值為[1,2,5,7],對(duì)應(yīng)的索引為[‘nu’, ‘li’, ‘xue’, ‘xi’] 創(chuàng)建一個(gè)名為dict_a的字典,字典中包含如下內(nèi)容{‘ting’:1, ‘shuo’:2, ‘du’:32, ‘xie’:44} 將dict_a字典轉(zhuǎn)化成名為series_b的series數(shù)組。 測試說明 如果答案正確,則會(huì)輸出 True 示例代碼如下:
# -*- coding: utf-8 -*-
from pandas import Series,DataFrame
import pandas as pd
def create_series():
'''
返回值:
series_a: 一個(gè)Series類型數(shù)據(jù)
series_b: 一個(gè)Series類型數(shù)據(jù)
dict_a: 一個(gè)字典類型數(shù)據(jù)
'''
# 請?jiān)诖颂砑哟a 完成本關(guān)任務(wù)
# ********** Begin *********#
series_a=Series([1,2,5,7],index=['nu','li','xue','xi'])
dict_a={'ting':1, 'shuo':2, 'du':32, 'xie':44}
series_b=Series(dict_a)
# ********** End **********#
# 返回series_a,dict_a,series_b
return series_a,dict_a,series_b
第2關(guān):了解數(shù)據(jù)處理對(duì)象-DataFrame
本關(guān)任務(wù) 創(chuàng)建一個(gè)五行三列的名為 df1 的DataFrame數(shù)組,列名為 [states,years,pops],行名[‘one’,‘two’,‘three’,‘four’,‘five’] 給df1添加新列,列名為 new_add,值為[7,4,5,8,2] 測試說明 如果答案正確,則會(huì)輸出 True 示例代碼如下:
# -*- coding: utf-8 -*-
from pandas import Series,DataFrame
import pandas as pd
def create_dataframe():
'''
返回值:
df1: 一個(gè)DataFrame類型數(shù)據(jù)
'''
# 請?jiān)诖颂砑哟a 完成本關(guān)任務(wù)
# ********** Begin *********#
dictionary = {'states':['0hio','0hio','0hio','Nevada','Nevada'],
'years':[2000,2001,2002,2001,2002],
'pops':[1.5,1.7,3.6,2.4,2.9]}
df1 = DataFrame(dictionary)
df1=DataFrame(dictionary,index=['one','two','three','four','five'])
df1['new_add']=[7,4,5,8,2]
# ********** End **********#
#返回df1
return df1
第3關(guān):讀取CSV格式數(shù)據(jù)
本關(guān)任務(wù) 將test3/uk_rain_2014.csv中的數(shù)據(jù)導(dǎo)入到df1中; 將列名修改為[‘water_year’,‘rain_octsep’,‘outflow_octsep’,‘rain_decfeb’, ‘outflow_decfeb’, ‘rain_junaug’, ‘outflow_junaug’]; 計(jì)算df1的總行數(shù)并存儲(chǔ)在length1中。 測試說明 如果答案正確,則會(huì)輸出 True 示例代碼如下:
# -*- coding: utf-8 -*-
from pandas import Series,DataFrame
import pandas as pd
def read_csv_data():
'''
返回值:
df1: 一個(gè)DataFrame類型數(shù)據(jù)
length1: 一個(gè)int類型數(shù)據(jù)
'''
# 請?jiān)诖颂砑哟a 完成本關(guān)任務(wù)
# ********** Begin *********#
df1 = pd.read_csv('test3/uk_rain_2014.csv', header=0)
df1.columns = ['water_year','rain_octsep','outflow_octsep','rain_decfeb', 'outflow_decfeb', 'rain_junaug', 'outflow_junaug']
length1=len(df1)
# ********** End **********#
#返回df1,length1
return df1,length1
第4關(guān):數(shù)據(jù)的基本操作——?jiǎng)h除
本關(guān)任務(wù) 在s1中刪除’z’行,并賦值到s2; 在d1中刪除’yy’列,并賦值到d2。 測試說明 如果答案正確,則會(huì)輸出 True 示例代碼如下:
# -*- coding: utf-8 -*-
from pandas import Series,DataFrame
import numpy as np
import pandas as pd
def delete_data():
'''
返回值:
s2: 一個(gè)Series類型數(shù)據(jù)
d2: 一個(gè)DataFrame類型數(shù)據(jù)
'''
# s1是Series類型數(shù)據(jù),d1是DataFrame類型數(shù)據(jù)
s1 = Series([5, 2, 4, 1], index=['v', 'x', 'y', 'z'])
d1=DataFrame(np.arange(9).reshape(3,3), columns=['xx','yy','zz'])
# 請?jiān)诖颂砑哟a 完成本關(guān)任務(wù)
# ********** Begin *********#
s2 = s1.drop('z')
d2 = d1.drop(['yy'],axis=1)
# ********** End **********#
# 返回s2,d2
return s2, d2
柚子快報(bào)激活碼778899分享:Pandas初體驗(yàn)
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。