日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python pandas csv读取_如何用 pandas 读取 csv 和 Excel 数据

發(fā)布時(shí)間:2023/12/15 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python pandas csv读取_如何用 pandas 读取 csv 和 Excel 数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文采用真實(shí)的股票數(shù)據(jù)作為案例,教你如何在Python中讀取常用的數(shù)據(jù)文件。

內(nèi)容:

讀取csv數(shù)據(jù)

讀取Excel數(shù)據(jù)

合并多張表

數(shù)據(jù)文件下載地址:

讀取csv數(shù)據(jù)

csv文件用逗號(hào)來(lái)分隔數(shù)值,是常用的數(shù)據(jù)格式之一,其具體形式可參考上面給出的數(shù)據(jù)文件。接下來(lái)我們將使用 Python 中的 pandas 數(shù)據(jù)分析包來(lái)進(jìn)行數(shù)據(jù)的讀取和查看。

pandas.read_csv(): 讀取csv格式數(shù)據(jù),并存儲(chǔ)成數(shù)據(jù)框 DataFrame 格式。

df.head(): 顯示數(shù)據(jù)框 df 的前5行。

df.info(): 顯示數(shù)據(jù)摘要。

# 導(dǎo)入pandas包

import pandas as pd

# 讀取csv文件

nasdaq = pd.read_csv('nasdaq-listings.csv')

# 顯示前10行數(shù)據(jù)

print(nasdaq.head(10))

Stock Symbol Company Name Last Sale Market Capitalization \

0 AAPL Apple Inc. 141.05 7.400000e+11

1 GOOGL Alphabet Inc. 840.18 5.810000e+11

2 GOOG Alphabet Inc. 823.56 5.690000e+11

3 MSFT Microsoft Corporation 64.95 5.020000e+11

4 AMZN Amazon.com, Inc. 884.67 4.220000e+11

5 FB Facebook, Inc. 139.39 4.030000e+11

6 CMCSA Comcast Corporation 37.14 1.760000e+11

7 INTC Intel Corporation 35.25 1.660000e+11

8 CSCO Cisco Systems, Inc. 32.42 1.620000e+11

9 AMGN Amgen Inc. 161.61 1.190000e+11

IPO Year Sector \

0 1980 Technology

1 NAN Technology

2 2004 Technology

3 1986 Technology

4 1997 Consumer Services

5 2012 Technology

6 NAN Consumer Services

7 NAN Technology

8 1990 Technology

9 1983 Health Care

Industry Last Update

0 Computer Manufacturing 4/26/17

1 Computer Software: Programming, Data Processing 4/24/17

2 Computer Software: Programming, Data Processing 4/23/17

3 Computer Software: Prepackaged Software 4/26/17

4 Catalog/Specialty Distribution 4/24/17

5 Computer Software: Programming, Data Processing 4/26/17

6 Television Services 4/26/17

7 Semiconductors 4/23/17

8 Computer Communications Equipment 4/23/17

9 Biotechnology: Biological Products (No Diagnos... 4/24/17

使用 .info() 方法,可查看數(shù)據(jù)框的摘要信息。

# 查看數(shù)據(jù)摘要

nasdaq.info()

RangeIndex: 1115 entries, 0 to 1114

Data columns (total 8 columns):

Stock Symbol 1115 non-null object

Company Name 1115 non-null object

Last Sale 1115 non-null float64

Market Capitalization 1115 non-null float64

IPO Year 1115 non-null object

Sector 1115 non-null object

Industry 1115 non-null object

Last Update 1115 non-null object

dtypes: float64(2), object(6)

memory usage: 69.8+ KB

從上面列出的信息可知,這份納斯達(dá)克股票數(shù)據(jù)包括股票代碼(Stock Symbol),公司名(Company Name),價(jià)格(Last Sale),市值(Market Capitalization),IPO年份(IPO Year), 行業(yè)(Sector), 產(chǎn)業(yè)(Industry),更新時(shí)間(Last Update)這8列。

讀取的數(shù)據(jù)需要能還原原始數(shù)據(jù)中的信息,比如 Last Update 應(yīng)該是時(shí)間格式的數(shù)據(jù),而在 IPO Year 中存在 NAN這類(lèi)缺失的數(shù)值,這些目前都沒(méi)有反應(yīng)出來(lái)。所以下面需要設(shè)置參數(shù),改進(jìn)csv文件的讀取方式。

pandas.read_csv() 參數(shù)

na_vlaues: 設(shè)置缺失值形式。

parse_dates: 將指定的列解析成時(shí)間日期格式。

nasdaq = pd.read_csv('nasdaq-listings.csv', na_values='NAN', parse_dates=['Last Update'])

print(nasdaq.head())

Stock Symbol Company Name Last Sale Market Capitalization \

0 AAPL Apple Inc. 141.05 7.400000e+11

1 GOOGL Alphabet Inc. 840.18 5.810000e+11

2 GOOG Alphabet Inc. 823.56 5.690000e+11

3 MSFT Microsoft Corporation 64.95 5.020000e+11

4 AMZN Amazon.com, Inc. 884.67 4.220000e+11

IPO Year Sector \

0 1980.0 Technology

1 NaN Technology

2 2004.0 Technology

3 1986.0 Technology

4 1997.0 Consumer Services

Industry Last Update

0 Computer Manufacturing 2017-04-26

1 Computer Software: Programming, Data Processing 2017-04-24

2 Computer Software: Programming, Data Processing 2017-04-23

3 Computer Software: Prepackaged Software 2017-04-26

4 Catalog/Specialty Distribution 2017-04-24

nasdaq.info()

RangeIndex: 1115 entries, 0 to 1114

Data columns (total 8 columns):

Stock Symbol 1115 non-null object

Company Name 1115 non-null object

Last Sale 1115 non-null float64

Market Capitalization 1115 non-null float64

IPO Year 593 non-null float64

Sector 1036 non-null object

Industry 1036 non-null object

Last Update 1115 non-null datetime64[ns]

dtypes: datetime64[ns](1), float64(3), object(4)

memory usage: 69.8+ KB

讀取Excel數(shù)據(jù)

Excel文件是傳統(tǒng)的數(shù)據(jù)格式,但面對(duì)海量數(shù)據(jù)時(shí),用編程的方法來(lái)處理數(shù)據(jù)更有優(yōu)勢(shì)。這里示例用的數(shù)據(jù)文件如下圖所示,注意它有3張sheet表。

類(lèi)似于csv文件,可以使用 pandas.read_excel() 函數(shù)來(lái)讀取 Excel 文件,并存儲(chǔ)成數(shù)據(jù)框格式。

pandas.read_excel() 讀取 Excel 文件,其參數(shù)如下:

sheet_name: 設(shè)置讀取的 sheet 名。

na_values: 設(shè)置缺失值的形式。

# 讀取Excel數(shù)據(jù),選取nyse這一頁(yè)

nyse = pd.read_excel('listings.xlsx', sheet_name='nyse', na_values='n/a')

# 顯示前幾行

print(nyse.head())

Stock Symbol Company Name Last Sale Market Capitalization \

0 DDD 3D Systems Corporation 14.48 1.647165e+09

1 MMM 3M Company 188.65 1.127366e+11

2 WBAI 500.com Limited 13.96 5.793129e+08

3 WUBA 58.com Inc. 36.11 5.225238e+09

4 AHC A.H. Belo Corporation 6.20 1.347351e+08

IPO Year Sector \

0 NaN Technology

1 NaN Health Care

2 2013.0 Consumer Services

3 2013.0 Technology

4 NaN Consumer Services

Industry

0 Computer Software: Prepackaged Software

1 Medical/Dental Instruments

2 Services-Misc. Amusement & Recreation

3 Computer Software: Programming, Data Processing

4 Newspapers/Magazines

# 顯示摘要信息

nyse.info()

RangeIndex: 3147 entries, 0 to 3146

Data columns (total 7 columns):

Stock Symbol 3147 non-null object

Company Name 3147 non-null object

Last Sale 3079 non-null float64

Market Capitalization 3147 non-null float64

IPO Year 1361 non-null float64

Sector 2177 non-null object

Industry 2177 non-null object

dtypes: float64(3), object(4)

memory usage: 172.2+ KB

這里其實(shí)只讀取了一張sheet表,但是該Excel文件一共有三張sheet表,下面將演示如何讀取所有的sheet表。

pd.ExcelFile():將 Excel 文件存儲(chǔ)成 ExcelFile 對(duì)象。

ExcelFile.sheet_names : 獲取ExcelFile的所有sheet表的名稱(chēng),存儲(chǔ)在python列表中。

# 將Excel文件讀取成 ExcelFile 格式

xls = pd.ExcelFile('listings.xlsx')

# 獲取sheet表的名稱(chēng)

exchanges = xls.sheet_names

print(exchanges)

['amex', 'nasdaq', 'nyse']

然后仍然使用 pd.read_excel() 讀取 ExcelFile 數(shù)據(jù),但傳遞給參數(shù) sheet_name 的值是上述 exchanges 列表。返回值 listings 是字典格式,每一個(gè)元素對(duì)應(yīng)的是一張表的DataFrame。

# 讀取所有sheet的數(shù)據(jù),存儲(chǔ)在字典中

listings = pd.read_excel(xls, sheet_name=exchanges, na_values='n/a')

# 查看 nasdaq 表的摘要信息

listings['nasdaq'].info()

RangeIndex: 3167 entries, 0 to 3166

Data columns (total 7 columns):

Stock Symbol 3167 non-null object

Company Name 3167 non-null object

Last Sale 3165 non-null float64

Market Capitalization 3167 non-null float64

IPO Year 1386 non-null float64

Sector 2767 non-null object

Industry 2767 non-null object

dtypes: float64(3), object(4)

memory usage: 173.3+ KB

合并多張表

現(xiàn)在我們希望將Excel中的多張表合并在一起,由于他們具有相同的列結(jié)構(gòu),所以可以進(jìn)行簡(jiǎn)單的堆疊。下面將 nyse 和 nasdaq 這兩張表連接在一起,使用 pd.concat() 函數(shù)。

pd.concat() 函數(shù)用于連接多個(gè)DataFrame數(shù)據(jù)框,注意這些被合并的DataFrame需要放在列表中。

# 讀取 nyse 和nasdaq 這兩張表的數(shù)據(jù)

nyse = pd.read_excel('listings.xlsx', sheet_name='nyse', na_values='n/a')

nasdaq = pd.read_excel('listings.xlsx', sheet_name='nasdaq', na_values='n/a')

# 添加一列 Exchange,標(biāo)記來(lái)自哪張表的數(shù)據(jù)

nyse['Exchange'] = 'NYSE'

nasdaq['Exchange'] = 'NASDAQ'

# 拼接兩個(gè)DataFrame

combined_listings = pd.concat([nyse, nasdaq]) # 注意這里的[ ]

combined_listings.info()

Int64Index: 6314 entries, 0 to 3166

Data columns (total 8 columns):

Stock Symbol 6314 non-null object

Company Name 6314 non-null object

Last Sale 6244 non-null float64

Market Capitalization 6314 non-null float64

IPO Year 2747 non-null float64

Sector 4944 non-null object

Industry 4944 non-null object

Exchange 6314 non-null object

dtypes: float64(3), object(5)

memory usage: 444.0+ KB

如果要合并所有的表,我們同樣可以通 ExcelFile.sheet_names 來(lái)獲取所有的sheet表的名稱(chēng),然后通過(guò)循環(huán)讀取每一個(gè)sheet表的數(shù)據(jù),最后使用 pd.concat() 函數(shù)來(lái)合并所有sheet表。

# 創(chuàng)建 ExcelFile 變量

xls = pd.ExcelFile('listings.xlsx')

# 獲取sheet名

exchanges = xls.sheet_names

# 創(chuàng)建空列表

listings = []

# 使用循環(huán)逐一導(dǎo)入每一頁(yè)的數(shù)據(jù),并存儲(chǔ)在列表中

for exchange in exchanges:

listing = pd.read_excel(xls, sheet_name=exchange, na_values='n/a')

listing['Exchange'] = exchange

listings.append(listing)

# 合并數(shù)據(jù)

listing_data = pd.concat(listings)

# 查看合并后數(shù)據(jù)的摘要

listing_data.info()

Int64Index: 6674 entries, 0 to 3146

Data columns (total 8 columns):

Stock Symbol 6674 non-null object

Company Name 6674 non-null object

Last Sale 6590 non-null float64

Market Capitalization 6674 non-null float64

IPO Year 2852 non-null float64

Sector 5182 non-null object

Industry 5182 non-null object

Exchange 6674 non-null object

dtypes: float64(3), object(5)

memory usage: 469.3+ KB

總結(jié)

以上是生活随笔為你收集整理的python pandas csv读取_如何用 pandas 读取 csv 和 Excel 数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。