Python數(shù)據(jù)分析numpy文本數(shù)據(jù)讀取索引切片實(shí)例詳解
數(shù)據(jù)導(dǎo)入和數(shù)組轉(zhuǎn)置
np.loadtxt(framme,dtype='dataType',delimmiter='分隔符',
skiprows=''(跳過的行數(shù)'),
usecols=''需要用到的行數(shù)',
unpack='Ture/Flase(是否轉(zhuǎn)置)':加載文本文件數(shù)據(jù)
loadtxt參數(shù)意義
- numpy數(shù)組轉(zhuǎn)置的是4種方法
- np.loadtxt中的參數(shù)unpack值設(shè)置為TRUE
- 使用數(shù)組的.T屬性進(jìn)行轉(zhuǎn)置
- 使用數(shù)組的transpose()方法進(jìn)行轉(zhuǎn)置
- 使用numpy數(shù)組的swapaxes方法
實(shí)例如下:
import numpy as np filepath = './doubantop250.csv' t1 = np.loadtxt(filepath,usecols=(1,2,3),delimiter=',',dtype='float') print(t1) # 轉(zhuǎn)置的四種方式 # first method:Set the value of parameter "unpack" —— True t2 = np.loadtxt(filepath,usecols=(1,2,3),delimiter=',',dtype='float',unpack=True) # second method: use the '.T' attributions of array's t3 = t1.T print(t3) # third method: use the method of 'transpose' t4 = t1.transpose() print(t4) # forth method: swapaxes(arguments:axes needed swapped) t5 = t1.swapaxes(0,1) print(t5)
運(yùn)行結(jié)果:
numpy數(shù)組索引與切片
import numpy as np filename = './doubantop250.csv' t1 = np.loadtxt(filename,delimiter=',',dtype='float',usecols=(1,2,3)) # print(t1) # 取行操作 print(t1[0]) print(t1[0,:]) # 取連續(xù)的多行 print(t1[3:]) print(t1[3:,:]) # 取不連續(xù)的多行 print(t1[[1,3,13,19]]) print(t1[[1,2,4,6],:]) # 取列 print(t1[:,0]) # 取連續(xù)的列 print(t1[:,2:]) # 取不連續(xù)的列 print(t1[:,[1,2]]) # 取第2-5行,2-3列 # 取多個位置的交叉數(shù)據(jù) print(t1[1:5,1:3]) # 取不相鄰的位置的數(shù)據(jù)信息 print(t1[[1,4,6],[0,1,2]])
import numpy as np filepath = './doubantop250.csv' t1 = np.loadtxt(filepath,delimiter=',',usecols=(1,2,3)) print(t1<9.5) t1[t1 < 9.5] = 0 print(t1[:,1]) # if-else操作 np.where(t1>=9.6,10,0) print(t1) # clip(m,n)把數(shù)組中小于m的替換成m,大于n的替換成n
以上就是Python數(shù)據(jù)分析numpy文本數(shù)據(jù)讀取索引切片實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python numpy數(shù)據(jù)讀取索引的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)可視化之Pyecharts使用詳解
Pyecharts是一個由百度開源的、用于生成Echarts圖表的類庫,可以用來進(jìn)行數(shù)據(jù)可視化分析。本文將詳細(xì)講解一下Pyecharts的使用,需要的可以參考一下2022-04-04Python使用cx_Freeze庫生成msi格式安裝文件的方法
這篇文章主要介紹了Python使用cx_Freeze庫生成msi格式安裝文件的方法,結(jié)合實(shí)例形式分析了Python基于cx_Freeze庫生成msi格式安裝文件操作技巧與相關(guān)問題解決方法,需要的朋友可以參考下2018-07-07用Python爬取英雄聯(lián)盟的皮膚詳細(xì)示例
大家好,本篇文章主要講的是用Python爬取英雄聯(lián)盟的皮膚詳細(xì)示例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Python使用Socket實(shí)現(xiàn)簡單聊天程序
這篇文章主要介紹了Python使用Socket實(shí)現(xiàn)簡單聊天程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02Python使用poplib模塊和smtplib模塊收發(fā)電子郵件的教程
smtplib模塊一般我們比較熟悉、這里我們會來講解使用smtplib發(fā)送SSL/TLS安全郵件的方法,而poplib模塊則負(fù)責(zé)處理接收pop3協(xié)議的郵件,下面我們就來看Python使用poplib模塊和smtplib模塊收發(fā)電子郵件的教程2016-07-07Python中shape[0]、shape[1]和shape[-1]分別的意思詳解(附代碼)
剛開始使用python做東西,總是不太理解矩陣、數(shù)組相關(guān)的問題,所以在此記錄shape方面的總結(jié),下面這篇文章主要給大家介紹了關(guān)于Python中shape[0]、shape[1]和shape[-1]分別是什么意思的相關(guān)資料,需要的朋友可以參考下2022-11-11