色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python Pandas 對列/行進行選擇,增加,刪除操作

瀏覽:45日期: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’])# 選擇其中一列進行顯示,列長度為最長列的長度# 除了 index 和 數據,還會顯示 列表頭名,和 數據 類型

運行結果:

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)# 增加列后進行顯示,其中 index 用于對應到該列 元素 位置(所以位置可以不由 列表 中的順序進行指定)print ('Adding a new column using the existing columns in DataFrame:')df[’four’]=df[’one’]+df[’two’]+df[’three’]print(df)# 我們選定列后,直接可以對整個列的元素進行批量運算操作,這里 NaN 與其他元素相加后,還是 NaN

運行結果:

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 函數)

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 函數print ('Deleting the first column using DEL function:')del(df[’one’])print(df)# 使用 pop 函數print ('Deleting another column using POP function:')df_2=df.pop(’two’) # 將一列 pop 到新的 dataframeprint(df_2)print(df)

運行結果:

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 通過 label 選擇行(loc 函數)

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’]) # 顯示這一行中,對應表頭 下的 對應數據,同時顯示 行 index 和 數據類型

運行結果:

one 2.0two 2.0Name: b, dtype: float64

2.1.2 通過序號選擇行(iloc 函數)

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]) # 序號 2 對應的是第 3 行的數據

運行結果:

one 3.0two 3.0Name: c, dtype: float64

2.1.3 通過序號選擇行切片

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 切片一致,不需要函數,直接切片即可

運行結果:

one twoc 3.0 3d NaN 4

2.2 增加行(append 函數)

# 通過 append 函數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) # 這里相當于把 第二個 dataframe 與第一個進行拼接,默認的 index 都是 0 1print(df.loc[0]) # 這里有兩行的 index 是 0

運行結果:

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

2.3 刪除行(drop 函數)

# 通過 drop 函數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) # 這里有兩個行標簽為 0,所以直接刪除了 2 行print(df)

運行結果:

a b1 3 41 7 8

到此這篇關于Python Pandas 對列/行進行選擇,增加,刪除操作的文章就介紹到這了,更多相關Python Pandas行列選擇增加刪除內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 麻豆影音 | 精品一区二区三区在线播放 | 欧美国产精品一区二区免费 | 日韩欧美印度一级毛片 | 在线看免费观看韩国特黄一级 | 成人高清无遮挡免费视频软件 | 日本免费一区二区三区视频 | 日韩精品一区二区三区在线观看 | 国产精品99久久免费观看 | 最新亚洲国产有精品 | 欧美日韩一区二区视频图片 | 女人一级特纯黄大片色 | 久久国内免费视频 | 在线视频亚洲一区 | 特黄特级a级黄毛片免费观看多人 | 91精品国产综合久久青草 | 欧美成人性色生活片天天看 | 久久99精品久久只有精品 | 日韩 国产 欧美 精品 在线 | 一级黄视频 | 成人欧美精品久久久久影院 | 成人久久 | 国产精品视频久久 | 国产精品免费综合一区视频 | 欧美ab片 | 波多野结衣一区二区 三区 波多野结衣一区二区三区88 | 亚洲三级a | 成年人午夜影院 | 午夜黄色毛片 | 美女与男人对肌免费网站 | 日韩欧美在线视频一区二区 | 一级成人毛片免费观看 | 波多野一区二区三区在线 | 欧美成人影院在线观看三级 | 日本香蕉一区二区三区 | 美女亚洲综合 | 欧美一二三 | 九九精品免费观看在线 | 国产成人精品午夜 | 视频网18免费 | 97精品福利视频在线 |