最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年05月17日 09:39:20   作者:AItrust  
這篇文章主要介紹了Python Pandas 對(duì)列/行進(jìn)行選擇,增加,刪除操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、列操作

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ù) 類(lèi)型

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

a    1.0
b    2.0
c    3.0
d    NaN
Name: 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 series

print ("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  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Adding a new column using the existing columns in DataFrame:
   one  two  three  four
a  1.0    1   10.0  12.0
b  2.0    2   20.0  24.0
c  3.0    3   30.0  36.0
d  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 到新的 dataframe
print(df_2)
print(df)

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

Our dataframe is:
   one  two  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Deleting the first column using DEL function:
   two  three
a    1   10.0
b    2   20.0
c    3   30.0
d    4    NaN
Deleting another column using POP function:
   three
a   10.0
b   20.0
c   30.0
d    NaN
POP column:
a    1
b    2
c    3
d    4
Name: 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ù)類(lèi)型

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

one    2.0
two    2.0
Name: 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.0
two    3.0
Name: 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  two
c  3.0    3
d  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 1
print(df.loc[0]) # 這里有兩行的 index 是 0

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

   a  b
0  1  2
1  3  4
0  5  6
1  7  8
   a  b
0  1  2
0  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  b
1  3  4
1  7  8

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

相關(guān)文章

  • Python對(duì)象與引用的介紹

    Python對(duì)象與引用的介紹

    今天小編就為大家分享一篇關(guān)于Python對(duì)象與引用的介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Python使用wxpy模塊實(shí)現(xiàn)微信兩兩群組消息同步功能(推薦)

    Python使用wxpy模塊實(shí)現(xiàn)微信兩兩群組消息同步功能(推薦)

    這篇文章主要介紹了Python使用wxpy模塊實(shí)現(xiàn)微信兩兩群組消息同步,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Pandas的數(shù)據(jù)過(guò)濾實(shí)現(xiàn)

    Pandas的數(shù)據(jù)過(guò)濾實(shí)現(xiàn)

    這篇文章主要介紹了Pandas的數(shù)據(jù)過(guò)濾實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Scrapy爬蟲(chóng)文件批量運(yùn)行的實(shí)現(xiàn)

    Scrapy爬蟲(chóng)文件批量運(yùn)行的實(shí)現(xiàn)

    這篇文章主要介紹了Scrapy爬蟲(chóng)文件批量運(yùn)行的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python-序列解包(對(duì)可迭代元素的快速取值方法)

    python-序列解包(對(duì)可迭代元素的快速取值方法)

    今天小編就為大家分享一篇python-序列解包(對(duì)可迭代元素的快速取值方法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • python開(kāi)發(fā)App基礎(chǔ)操作API使用示例過(guò)程

    python開(kāi)發(fā)App基礎(chǔ)操作API使用示例過(guò)程

    這篇文章主要為大家介紹了python開(kāi)發(fā)App基礎(chǔ)操作API使用的示例過(guò)程有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-10-10
  • Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出示例

    Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出示例

    這篇文章主要介紹了Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出,涉及Python文件讀取、字符串替換、分割以及字典遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例

    Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法,結(jié)合實(shí)例形式分析了Python基于第三方庫(kù)pdfminier實(shí)現(xiàn)針對(duì)pdf格式文檔的讀取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Python實(shí)現(xiàn)12306自動(dòng)搶火車(chē)票功能

    Python實(shí)現(xiàn)12306自動(dòng)搶火車(chē)票功能

    一到國(guó)慶、春節(jié)這種長(zhǎng)假,搶火車(chē)票就非常困難?各大互聯(lián)網(wǎng)公司都推出搶票服務(wù),只要加錢(qián)給服務(wù)費(fèi)就可以增加搶到票的幾率。本文將和你一起探索搶票軟件背后的原理。
    2021-12-12
  • 關(guān)于jieba.cut與jieba.lcut的區(qū)別及說(shuō)明

    關(guān)于jieba.cut與jieba.lcut的區(qū)別及說(shuō)明

    這篇文章主要介紹了關(guān)于jieba.cut與jieba.lcut的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評(píng)論

浦东新区| 商洛市| 石泉县| 松江区| 安岳县| 定南县| 阜康市| 盘锦市| 盐边县| 南昌县| 岑巩县| 宜川县| 海兴县| 通化市| 金川县| 思南县| 固原市| 海林市| 青神县| 临安市| 北碚区| 新龙县| 宁城县| 嵩明县| 桃园县| 三原县| 黔西县| 沛县| 乐亭县| 泰来县| 密云县| 湘阴县| 泸定县| 休宁县| 峨山| 钟山县| 邮箱| 厦门市| 垫江县| 定西市| 沁水县|