婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Pandas讀取行列數據最全方法

Pandas讀取行列數據最全方法

熱門標簽:西青語音電銷機器人哪家好 電梯新時達系統外呼顯示e 宿州電話機器人哪家好 無錫智能外呼系統好用嗎 地圖標注與注銷 南昌地圖標注 旅游廁所地圖標注怎么弄 百應電話機器人總部 成都呼叫中心外呼系統哪家強

1、讀取方法有按行(單行,多行連續,多行不連續),按列(單列,多列連續,多列不連續);部分不連續行不連續列;按位置(坐標),按字符(索引);按塊(list);函數有 df.iloc(), df.loc(), df.iat(), df.at(), df.ix()。

2、轉換為DF,賦值columns,index,修改添加數據,取行列索引

data = {'省份': ['北京', '上海', '廣州', '深圳'],
        '年份': ['2017', '2018', '2019', '2020'],
        '總人數': ['2200', '1900', '2170', '1890'],
        '高考人數': ['6.3', '5.9', '6.0', '5.2']}
df = pd.DataFrame(data, columns=['省份', '年份', '總人數', '高考人數', '高數'],
                  index=['one', 'two', 'three', 'four'])
df['高數'] = ['90', '95', '92', '98']
print("行索引:{}".format(list(df.index)))
print("列索引:{}".format(list(df.columns)))
print(df.index[1:3])
print(df.columns[1])
print(df.columns[1:3])
print(df)

行索引:['one', 'two', 'three', 'four']
列索引:['省份', '年份', '總人數', '高考人數', '高數']
Index(['two', 'three'], dtype='object')
年份
Index(['年份', '總人數'], dtype='object')
       省份    年份   總人數 高考人數  高數
one    北京  2017  2200  6.3  90
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
four   深圳  2020  1890  5.2  98

3、iloc不能通過[:, [1:3]]取連續數據,取連續數據只能通過 df[df.columns[1:4]],先獲取列索引,再取數據。

print(df['省份'])  #按列名取列
print(df.省份)  #按列名取列
print(df[['省份', '總人數']])  #按列名取不連續列數據
print(df[df.columns[1:4]])  #按列索引取連續列數據
print(df.iloc[:, 1])  #按位置取列
print(df.iloc[:, [1, 3]])  #按位置取不連續列數據

one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
       省份   總人數
one    北京  2200
two    上海  1900
three  廣州  2170
four   深圳  1890
         年份   總人數 高考人數
one    2017  2200  6.3
two    2018  1900  5.9
three  2019  2170  6.0
four   2020  1890  5.2
one      2017
two      2018
three    2019
four     2020
Name: 年份, dtype: object
         年份 高考人數
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2

4、通過df.iloc[](數字)取行數據,取部分行部分列時,要先寫行,再寫列;有條件的取數據

print(df[1:3])  #按行取數據,這行代碼結果沒在下面輸出
print(df[df.高數>90])  #按行有條件的取數據,結果沒輸出
print(df.iloc[1])  #按行取行數據
print(df.iloc[1, 3])  #按坐標取
print(df.iloc[[1], [3]])  #按坐標取
print(df.loc[df.index[1:3]])  #按行索引取行,但沒必要
print(df.iloc[1:3])  #按行取連續數據
print(df.iloc[[1, 3]])  按行取不連續數據
print(df.iloc[[1,2,3], [2,4]])  取部分行部分列數據

省份        上海
年份      2018
總人數     1900
高考人數     5.9
高數        95
Name: two, dtype: object
5.9
    高考人數
two  5.9
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
      省份    年份   總人數 高考人數  高數
two   上海  2018  1900  5.9  95
four  深圳  2020  1890  5.2  98
        總人數  高數
two    1900  95
three  2170  92
four   1890  98

5、通過df.loc[]索引(字符)取行數據。

print(df.loc['two'])
print(df.loc['two', '省份'])
print(df.loc['two':'three'])
print(df.loc[['one', 'three']])
print(df.loc[['one', 'three'], ['省份', '年份']])

省份        上海
年份      2018
總人數     1900
高考人數     5.9
高數        95
Name: two, dtype: object
上海
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總人數 高考人數  高數
one    北京  2017  2200  6.3  90
three  廣州  2019  2170  6.0  92
       省份    年份
one    北京  2017
three  廣州  2019

6、ix,iat,at取行列數據,此方法不常用,可以使用上面方法即可。

print(df.ix[1:3])
print(df.ix[:, [1, 3]])
print(df.iat[1,3])
print(df.at['two', '省份'])

       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
         年份 高考人數
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2
5.9
上海

到此這篇關于Pandas讀取行列數據最全方法的文章就介紹到這了,更多相關Pandas讀取行列 內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • pandas 轉換成行列表進行讀取與Nan處理的方法
  • pandas Dataframe行列讀取的實例

標簽:辛集 濰坊 渭南 七臺河 雅安 贛州 許昌 西安

巨人網絡通訊聲明:本文標題《Pandas讀取行列數據最全方法》,本文關鍵詞  Pandas,讀取,行列,數據,最全,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Pandas讀取行列數據最全方法》相關的同類信息!
  • 本頁收集關于Pandas讀取行列數據最全方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 聂拉木县| 莲花县| 南部县| 新昌县| 龙口市| 岳阳市| 哈尔滨市| 永城市| 阿勒泰市| 太仆寺旗| 巴彦淖尔市| 陆良县| 绿春县| 宣威市| 正镶白旗| 高青县| 涞水县| 云浮市| 西乌珠穆沁旗| 突泉县| 翁源县| 屯门区| 安远县| 荆州市| 麻江县| 孟州市| 大埔县| 颍上县| 姜堰市| 高平市| 公主岭市| 襄垣县| 茶陵县| 手游| 会同县| 广平县| 山东省| 宝兴县| 鹿泉市| 大港区| 香河县|