国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作

瀏覽:54日期:2022-07-25 11:24:11

一、列操作

1.1 選擇列

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’])}df = pd.DataFrame(d)print (df [’one’])# 選擇其中一列進(jìn)行顯示,列長(zhǎng)度為最長(zhǎng)列的長(zhǎng)度# 除了 index 和 數(shù)據(jù),還會(huì)顯示 列表頭名,和 數(shù)據(jù) 類型

運(yùn)行結(jié)果:

a 1.0b 2.0c 3.0d NaNName: one, dtype: float64

1.2 增加列

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’])}df = pd.DataFrame(d)# Adding a new column to an existing DataFrame object with column label by passing new seriesprint ('Adding a new column by passing as Series:')df[’three’]=pd.Series([10,30,20],index=[’a’,’c’,’b’])print(df)# 增加列后進(jìn)行顯示,其中 index 用于對(duì)應(yīng)到該列 元素 位置(所以位置可以不由 列表 中的順序進(jìn)行指定)print ('Adding a new column using the existing columns in DataFrame:')df[’four’]=df[’one’]+df[’two’]+df[’three’]print(df)# 我們選定列后,直接可以對(duì)整個(gè)列的元素進(jìn)行批量運(yùn)算操作,這里 NaN 與其他元素相加后,還是 NaN

運(yùn)行結(jié)果:

Adding a new column by passing as Series: one two threea 1.0 1 10.0b 2.0 2 20.0c 3.0 3 30.0d NaN 4 NaNAdding a new column using the existing columns in DataFrame: one two three foura 1.0 1 10.0 12.0b 2.0 2 20.0 24.0c 3.0 3 30.0 36.0d NaN 4 NaN NaN

1.3 刪除列(del 和 pop 函數(shù))

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’]), ’three’ : pd.Series([10,20,30], index=[’a’,’b’,’c’])}df = pd.DataFrame(d)print ('Our dataframe is:')print(df)# 使用 del 函數(shù)print ('Deleting the first column using DEL function:')del(df[’one’])print(df)# 使用 pop 函數(shù)print ('Deleting another column using POP function:')df_2=df.pop(’two’) # 將一列 pop 到新的 dataframeprint(df_2)print(df)

運(yùn)行結(jié)果:

Our dataframe is: one two threea 1.0 1 10.0b 2.0 2 20.0c 3.0 3 30.0d NaN 4 NaNDeleting the first column using DEL function: two threea 1 10.0b 2 20.0c 3 30.0d 4 NaNDeleting another column using POP function: threea 10.0b 20.0c 30.0d NaNPOP column:a 1b 2c 3d 4Name: two, dtype: int64

二、行操作

2.1 選擇行

2.1.1 通過(guò) label 選擇行(loc 函數(shù))

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’])}df = pd.DataFrame(d)print(df.loc[’b’]) # 顯示這一行中,對(duì)應(yīng)表頭 下的 對(duì)應(yīng)數(shù)據(jù),同時(shí)顯示 行 index 和 數(shù)據(jù)類型

運(yùn)行結(jié)果:

one 2.0two 2.0Name: b, dtype: float64

2.1.2 通過(guò)序號(hào)選擇行(iloc 函數(shù))

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’])}df = pd.DataFrame(d)print(df.iloc[2]) # 序號(hào) 2 對(duì)應(yīng)的是第 3 行的數(shù)據(jù)

運(yùn)行結(jié)果:

one 3.0two 3.0Name: c, dtype: float64

2.1.3 通過(guò)序號(hào)選擇行切片

d = {’one’ : pd.Series([1, 2, 3], index=[’a’, ’b’, ’c’]), ’two’ : pd.Series([1, 2, 3, 4], index=[’a’, ’b’, ’c’, ’d’])}df = pd.DataFrame(d)print(df[2:4]) # 這里選擇第 3 到 第 4 行,與 Python 切片一致,不需要函數(shù),直接切片即可

運(yùn)行結(jié)果:

one twoc 3.0 3d NaN 4

2.2 增加行(append 函數(shù))

# 通過(guò) append 函數(shù)df = pd.DataFrame([[1, 2], [3, 4]], columns = [’a’,’b’])df2 = pd.DataFrame([[5, 6], [7, 8]], columns = [’a’,’b’])df = df.append(df2)print(df) # 這里相當(dāng)于把 第二個(gè) dataframe 與第一個(gè)進(jìn)行拼接,默認(rèn)的 index 都是 0 1print(df.loc[0]) # 這里有兩行的 index 是 0

運(yùn)行結(jié)果:

a b0 1 21 3 40 5 61 7 8 a b0 1 20 5 6

2.3 刪除行(drop 函數(shù))

# 通過(guò) drop 函數(shù)df = pd.DataFrame([[1, 2], [3, 4]], columns = [’a’,’b’])df2 = pd.DataFrame([[5, 6], [7, 8]], columns = [’a’,’b’])df = df.append(df2)df = df.drop(0) # 這里有兩個(gè)行標(biāo)簽為 0,所以直接刪除了 2 行print(df)

運(yùn)行結(jié)果:

a b1 3 41 7 8

到此這篇關(guān)于Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作的文章就介紹到這了,更多相關(guān)Python Pandas行列選擇增加刪除內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 三级伦理网站 | 欧美午夜不卡在线观看最新 | 国产一区二区三区不卡免费观看 | 一级特黄aaa大片免费看 | 性高湖久久久久久久久aaaaa | 亚洲第一男人天堂 | 欧美一级网站 | 亚洲无线一二三区2021 | 欧美精品亚洲精品日韩一区 | 国产日韩精品视频一区二区三区 | 最新国产成人综合在线观看 | 亚洲男人天堂2018 | 亚洲欧美视频在线播放 | 性盈盈影院影院67194 | 久久综合伊人77777 | 美女18一级毛片免费看 | 九九免费精品视频 | 欧美色操| 日韩午夜视频在线观看 | 欧美日韩一区二区三区高清不卡 | 深夜福利视频在线观看免费播放 | www.日本在线观看 | 国内精品不卡一区二区三区 | 蜜桃日本一道无卡不码高清 | 一 级 黄 色 片生活片 | 香港经典a毛片免费观看看 香港经典a毛片免费观看爽爽影院 | 亚洲精品91 | 玖草资源在线 | 欧美熟videos肥婆 | 正在播放国产大学生情侣 | 草草久久97超级碰碰碰免费 | 92手机看片福利永久国产 | 日韩久久精品 | a大片久久爱一级 | 国产美女一区二区在线观看 | 亚洲国产精品欧美日韩一区二区 | 亚洲欧美久久精品一区 | 国产a一级毛片含羞草传媒 国产a自拍 | 91福利国产在线观看香蕉 | 在线看亚洲 | 天天黄色片 |