国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python中Pandas之DataFrame索引、選取數(shù)據(jù)

這篇具有很好參考價值的文章主要介紹了python中Pandas之DataFrame索引、選取數(shù)據(jù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


總結一下 DataFrame索引問題

1.索引是什么

1.1 認識索引

先創(chuàng)建一個簡單的DataFrame。

myList = [['a', 10, 1.1],
	  ['b', 20, 2.2],
	  ['c', 30, 3.3],
	  ['d', 40, 4.4]]  
df1 = pd.DataFrame(data = myList)
print(df1)
--------------------------------
[out]:
   0   1    2
0  a  10  1.1
1  b  20  2.2
2  c  30  3.3
3  d  40  4.4

DataFrame中有兩種索引:

  • 行索引(index):對應最左邊那一豎列
  • 列索引(columns):對應最上面那一橫行

兩種索引默認均為從0開始的自增整數(shù)。

# 輸出行索引
print(df1.index)
[out]:
RangeIndex(start=0, stop=4, step=1)
---------------------------------------
# 輸出列索引
print(df1.columns)
[out]:
RangeIndex(start=0, stop=3, step=1)
---------------------------------------
# 輸出所有的值
print(df1.values)
[out]:
array([['a', 10, 1.1],
       ['b', 20, 2.2],
       ['c', 30, 3.3],
       ['d', 40, 4.4]], dtype=object)

1.2 自定義索引

可以使用 index 這個參數(shù)指定行索引,columns 這個參數(shù)指定列索引。

df2 = pd.DataFrame(myList, 
		           index = ['one', 'two', 'three', 'four'], 
		           columns = ['char', 'int', 'float'])
print(df2)
-----------------------------------------------------------
[out]:
      char  int  float
one      a   10    1.1
two      b   20    2.2
three    c   30    3.3
four     d   40    4.4

輸出此時的行索引和列索引:

# 輸出行索引
print(df2.index)
[out]:
Index(['one', 'two', 'three', 'four'], dtype='object')
--------------------------------------------------------
# 輸出列索引
print(df2.columns)
[out]:
Index(['char', 'int', 'float'], dtype='object')

2. 索引的簡單使用

2.1 列索引

  • 選擇一列:
print(df2['char'])
print(df2.char)
# 兩種方式輸出一樣
[out]:
one      a
two      b
three    c
four     d
Name: char, dtype: object

注意此時方括號里面只傳入一個字符串 ’char’,這樣選出來的一列,結果的類型為 Series

type(df2['char'])
[out]: pandas.core.series.Series
  • 選擇多列:
print(df2[['char', 'int']])
[out]: 
      char   int
one      a   10
two      b   20
three    c   30
four     d   40

注意此時方括號里面?zhèn)魅胍粋€列表 [‘char’, ‘int’],選出的結果類型為 DataFrame。
如果只想選出來一列,卻想返回 DataFrame 類型怎么辦?

print(df2[['char']])
[out]:
      char
one      a
two      b
three    c
four     d
---------------------------------------
type(df2[['char']])
[out]:pandas.core.frame.DataFrame

注意直接使用 df2[0] 取某一列會報錯,除非columns是由下標索引組成的,比如df1那個樣子,df1[0] 就不會報錯。

print(df1[0])
[out]:
0    a
1    b
2    c
3    d
Name: 0, dtype: object
-----------------------
print(df2[0])
[out]: 
KeyError: 0

2.1.2 使用loc和iloc

df = dat_df.iloc[:, [0, 2, 3, 4]]  #選擇所有行,并選擇第0,2,3,4列,列名可以為其它字符串

2.2 行索引

2.2.1 使用[ : ]

區(qū)別于選取列,此種方式 [ ] 中不再單獨的傳入一個字符串,而是需要使用冒號切片。

  • 選取行標簽’two’’three’ 的多行數(shù)據(jù)
print(df2['two': 'three'])
[out]:
      char  int  float
two      b   20    2.2
three    c   30    3.3
# dataframe格式
# 也可以直接用數(shù)字
  • 選取行標簽為 ’two’ 這一行數(shù)據(jù)
# 此時返回的類型為DataFrame
print(df2['two': 'two'])
[out]:
      char  int  float
two      b   20    2.2

[ ] 中不僅可以傳入行標簽,還可以傳入行的編號。

  • 選取從第1行到第3行的數(shù)據(jù)(編號從0開始)
print(df2[1:4])
[out]:
      char  int  float
two      b   20    2.2
three    c   30    3.3
four     d   40    4.4
# dataframe格式

可以看到選取的數(shù)據(jù)是不包含方括號最右側的編號所對應的數(shù)據(jù)的。

  • 選取第1行的數(shù)據(jù)
print(df2[1:2])
[out]:
    char  int  float
two    b   20    2.2

2.2.2 使用.loc()和.iloc()

區(qū)別就是 .loc() 是根據(jù)行索引和列索引的值來選取數(shù)據(jù),而 .iloc() 是根據(jù)從 0 開始的下標位置來進行索引的。

  • 選取
    1. 使用.loc()
print(df2.loc['one'])
[out]:
char       a
int       10
float    1.1
Name: one, dtype: object
-------------------------------------------
print(df2.loc[['one', 'three']])
[out]:
      char  int  float
one      a   10    1.1
three    c   30    3.3
-------------------------------------------
df2.loc['one': 'three']
Out[14]: 
      char  int  float
one      a   10    1.1
two      b   20    2.2
three    c   30    3.3

2. 使用.iloc()

print(df2.iloc[0])
[out]:
char       a
int       10
float    1.1
Name: one, dtype: object
-------------------------------------------
print(df2.iloc[[0, 2]])
[out]:
      char  int  float
one      a   10    1.1
three    c   30    3.3
-------------------------------------------
df2.iloc[1: 3]
Out[18]: 
      char  int  float
two      b   20    2.2
three    c   30    3.3

3. 根據(jù)列條件,選取dataframe數(shù)據(jù)框中的數(shù)據(jù)

# 選取等于某些值的行記錄 用 == 

df.loc[df['column_name'] == some_value]

# 選取某列是否是某一類型的數(shù)值 用 isin

df.loc[df['column_name'].isin(some_values)]

# 多種條件的選取 用 &

df.loc[(df['column'] == some_value) & df['other_column'].isin(some_values)]

# 選取不等于某些值的行記錄 用 !=

df.loc[df['column_name'] != some_value]

# isin返回一系列的數(shù)值,如果要選擇不符合這個條件的數(shù)值使用~

df.loc[~df['column_name'].isin(some_values)]

4. 根據(jù)列條件,獲取行索引號并轉成列表

dataframe中根據(jù)一定的條件,得到符合要求的某些行元素所在的位置

import pandas as pd
df = pd.DataFrame({'BoolCol': [1, 2, 3, 3, 4],'attr': [22, 33, 22, 44, 66]},  
       index=[10,20,30,40,50])  
print(df)  
a = df[(df.BoolCol==3)&(df.attr==22)].index.tolist()  
print(a)  

輸出:

  BoolCol  attr  
10        1    22  
20        2    33  
30        3    22  
40        3    44  
50        4    66  
[30]  

注意:
df[(df.BoolCol==3)&(df.attr==22)].index 返回的是 index 對象列表,需轉換為普通列表格式時用 tolist() 方法

5. 索引操作集錦

a = data_1H2['num'].value_counts(sort=True, ascending=True).sort_index(ascending=False)

參考鏈接
[1] Pandas中DataFrame索引、選取數(shù)據(jù) 2020.3文章來源地址http://www.zghlxwxcb.cn/news/detail-402309.html

到了這里,關于python中Pandas之DataFrame索引、選取數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【python】pandas-DataFrame類型數(shù)據(jù)重命名列表頭

    【python】pandas-DataFrame類型數(shù)據(jù)重命名列表頭

    目錄 0.環(huán)境 1.將DataFrame類型數(shù)據(jù)某一列重命名 windows + jupyter notebook + python? 使用場景: 在處理數(shù)據(jù)對齊的問題時,兩個表格的對齊列名不相同(一個數(shù)據(jù)集是DataFrame類型,一個數(shù)據(jù)集是geopandas類型),所以想修改一下DataFrame類型數(shù)據(jù)的某一列名字,特此記錄分享 1)重命名前

    2024年02月14日
    瀏覽(35)
  • Python 之 Pandas DataFrame 數(shù)據(jù)類型的簡介、創(chuàng)建的列操作

    Python 之 Pandas DataFrame 數(shù)據(jù)類型的簡介、創(chuàng)建的列操作

    DataFrame 是 Pandas 的重要數(shù)據(jù)結構之一,也是在使用 Pandas 進行數(shù)據(jù)分析過程中最常用的結構之一,可以這么說,掌握了 DataFrame 的用法,你就擁有了學習數(shù)據(jù)分析的基本能力。 DataFrame 是一個表格型的數(shù)據(jù)結構,既有行標簽(index),又有列標簽(columns),它也被稱異構數(shù)據(jù)表

    2024年02月06日
    瀏覽(24)
  • 4.11 Pandas中的DataFrame數(shù)據(jù)類型API函數(shù)參考手冊(一) (Python)

    前言 Pandas 是一個專門用于數(shù)據(jù)處理和分析的 Python 庫,它提供了眾多強大的數(shù)據(jù)結構和函數(shù),幫助用戶更加輕松、高效地完成數(shù)據(jù)處理和分析任務。其中,DataFrame 數(shù)據(jù)類型是 Pandas 中非常重要的一種數(shù)據(jù)結構,可以方便地對二維表格數(shù)據(jù)進行操作、處理、統(tǒng)計和可視化等工作

    2024年02月10日
    瀏覽(22)
  • python的pandas中如何在dataframe中插入一行或一列數(shù)據(jù)?

    python的pandas中如何在dataframe中插入一行或一列數(shù)據(jù)?

    dataframe類型是如何插入一行或一列數(shù)據(jù)的呢?這個需求在本文中將會進行討論。相比較ndarray類型的同樣的“數(shù)據(jù)插入”需求,dataframe的實現(xiàn)方式,則不是很好用。本文以一個dataframe類型變量為例,測試插入一行數(shù)據(jù)或者一列數(shù)據(jù)的方式方法。測試環(huán)境:win10,python@3.11.0,nu

    2024年02月03日
    瀏覽(35)
  • 【python】數(shù)據(jù)可視化,使用pandas.merge()對dataframe和geopandas類型數(shù)據(jù)進行數(shù)據(jù)對齊

    【python】數(shù)據(jù)可視化,使用pandas.merge()對dataframe和geopandas類型數(shù)據(jù)進行數(shù)據(jù)對齊

    目錄 0.環(huán)境 1.適用場景 2.pandas.merge()函數(shù)詳細介紹 3.名詞解釋“數(shù)據(jù)對齊”(來自chatGPT3.5) 4.本文將給出兩種數(shù)據(jù)對齊的例子 1)dataframe類型數(shù)據(jù)和dataframe類型數(shù)據(jù)對齊(對齊NAME列); 數(shù)據(jù)對齊前的兩組數(shù)據(jù)集: 數(shù)據(jù)對齊后的數(shù)據(jù)集(通過pandas.merge()函數(shù)對齊): 代碼 2)

    2024年02月09日
    瀏覽(40)
  • Pandas教程:如何使用insert函數(shù)向Dataframe指定位置插入新的數(shù)據(jù)列(Python)

    Pandas教程:如何使用insert函數(shù)向Dataframe指定位置插入新的數(shù)據(jù)列(Python) Pandas是Python中最流行的數(shù)據(jù)處理和分析庫之一。在數(shù)據(jù)分析過程中,有時候需要在Dataframe中插入新的數(shù)據(jù)列。在本教程中,我們將介紹如何使用Pandas的insert函數(shù)在指定位置插入新的數(shù)據(jù)列。 首先,我們

    2024年02月11日
    瀏覽(26)
  • 【Python】數(shù)據(jù)科學工具(Numpy Pandas np.array() 創(chuàng)建訪問數(shù)組 向量與矩陣 Series DataFrame)

    1.Numpy numpy是Python中一個非常重要的科學計算庫,其最基礎的功能就是N維數(shù)組對象——ndarray。 1.1 數(shù)組的創(chuàng)建 1)np.array() 用 np.array() 函數(shù)可以將Python的序列對象(如列表、元組)轉換為ndarray數(shù)組。 2)arange、linspace、logspace np.arange(start, stop, step) :創(chuàng)建一個一維數(shù)組,其中的值

    2024年02月10日
    瀏覽(20)
  • 如何使用Python的pandas庫獲取DataFrame數(shù)據(jù)的最小值、最大值以及自定義分位數(shù)?

    Pandas是一個非常流行的Python數(shù)據(jù)處理庫,它提供了大量的方法和工具來處理和分析數(shù)據(jù)。在本文中,我將向您展示如何使用Pandas獲取dataframe格式數(shù)據(jù)的最小值、最大值和自定義分位數(shù)。 1、 獲取最小值和最大值 獲取dataframe的最小值和最大值非常簡單??梢允褂肞andas的min()和

    2024年02月02日
    瀏覽(21)
  • 「Python|Pandas|場景案例」如何只保留DataFrame數(shù)據(jù)集的某些列(要保留的列不固定)

    本文主要介紹在使用pandas進行數(shù)據(jù)分析過程中的數(shù)據(jù)預處理時,如果希望僅保留某些列的數(shù)據(jù)需要如何操作。同時介紹一些特殊情況,比如列是用變量存儲;或者列是一個全集,處理的數(shù)據(jù)集中不一定包括列出的全部列名。 在數(shù)據(jù)處理的時候,可能會遇到數(shù)據(jù)集包含的數(shù)據(jù)字

    2024年02月06日
    瀏覽(34)
  • 【第三章 Python 機器學習入門之Series和DataFrame的創(chuàng)建、索引、切片、數(shù)據(jù)清洗、數(shù)據(jù)分析等】

    【第三章 Python 機器學習入門之Series和DataFrame的創(chuàng)建、索引、切片、數(shù)據(jù)清洗、數(shù)據(jù)分析等】

    第一章 Python 機器學習入門之Pandas庫的使用 第二章 Python 機器學習入門之NumPy庫的使用 第四章 Python 機器學習入門之數(shù)據(jù)可視化 第五章 Python 機器學習入門之機器學習算法 第六章 Python 機器學習入門之實戰(zhàn)項目 Series是一種一維數(shù)組,可以通過以下方式創(chuàng)建: 通過列表創(chuàng)建Ser

    2024年02月05日
    瀏覽(96)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包