濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python讀寫(xiě)excel數(shù)據(jù)--pandas詳解

python讀寫(xiě)excel數(shù)據(jù)--pandas詳解

熱門(mén)標(biāo)簽:徐州天音防封電銷(xiāo)卡 佛山防封外呼系統(tǒng)收費(fèi) 鄭州智能外呼系統(tǒng)運(yùn)營(yíng)商 哈爾濱外呼系統(tǒng)代理商 不錯(cuò)的400電話(huà)辦理 湛江電銷(xiāo)防封卡 南昌辦理400電話(huà)怎么安裝 獲客智能電銷(xiāo)機(jī)器人 電話(huà)機(jī)器人適用業(yè)務(wù)

一、讀寫(xiě)excel數(shù)據(jù)

利用pandas可以很方便的讀寫(xiě)excel數(shù)據(jù)

1.1 讀:

data_in = pd.read_excel('M2FENZISHI.xlsx')

1.2寫(xiě):

首先要?jiǎng)?chuàng)建數(shù)據(jù)框

# example
df = pd.DataFrame({'A':[0,1,2]})
writer = pd.ExcelWriter('test.xlsx') #name of excel file
df.to_excel(writer, sheet_name='Sheet1') # write
writer.save()   # save

二、舉例

2.1 要求

這個(gè)例子稍微有點(diǎn)復(fù)雜,只看讀和寫(xiě)的部分就可以了。

例子要實(shí)現(xiàn)的目標(biāo)為:有一個(gè)excel文件,如下:


現(xiàn)在要將里面的化學(xué)符號(hào)中數(shù)字和字母分開(kāi),得到如下結(jié)果

2.2 實(shí)現(xiàn)

由于化學(xué)符號(hào)里面有數(shù)字和字母,要提取數(shù)字或者字母首先想到的是正則表達(dá)式re模塊。

在讀取時(shí)由于我們已經(jīng)將第一列命名data,因此pandas可以直接只讀這一列的提名。

讀取數(shù)字可以使用re.compile實(shí)現(xiàn),如:

下面是完整實(shí)現(xiàn)的代碼

import numpy as np 
import re 
import pandas as pd
data_in = pd.read_excel('M2FENZISHI.xlsx')['data'] #load data
print(data_in.shape)
length = len(data_in)    # length
pattern = re.compile(r'\d+') # find number
num_out = []
for i in range(length):
    temp = pattern.findall(data_in[i]) # find number
    int_num = list(map(int,temp))
    num_out.append(int_num)
num_out = np.array(num_out)
print(num_out.shape)
# writer data to excel
df = pd.DataFrame({'C': num_out[:, 0], 'H': num_out[:, 1], 'O': num_out[:, 2],
                   'N': num_out[:, 3], 'P': num_out[:, 4], 'S': num_out[:, 5]})
writer = pd.ExcelWriter('test.xlsx') # name of the file
df.to_excel(writer, sheet_name='Sheet1')
writer.save()

結(jié)果如下:

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

您可能感興趣的文章:
  • Python 讀寫(xiě) Matlab Mat 格式數(shù)據(jù)的操作
  • 解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題
  • python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)
  • python里讀寫(xiě)excel等數(shù)據(jù)文件的6種常用方式(小結(jié))
  • python讀寫(xiě)數(shù)據(jù)讀寫(xiě)csv文件(pandas用法)
  • Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)讀寫(xiě)分離的示例

標(biāo)簽:蕪湖 蘭州 廣西 呂梁 紹興 安康 懷化 吉安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python讀寫(xiě)excel數(shù)據(jù)--pandas詳解》,本文關(guān)鍵詞  python,讀寫(xiě),excel,數(shù)據(jù),--pandas,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python讀寫(xiě)excel數(shù)據(jù)--pandas詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于python讀寫(xiě)excel數(shù)據(jù)--pandas詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    开原市| 鹤岗市| 吉木萨尔县| 宾阳县| 大丰市| 搜索| 恩施市| 静海县| 东兰县| 综艺| 民丰县| 辰溪县| 偏关县| 砚山县| 花垣县| 无锡市| 社旗县| 青川县| 吐鲁番市| 定西市| 苗栗县| 南涧| 易门县| 固始县| 汽车| 监利县| 永州市| 青铜峡市| 德惠市| 莲花县| 丹棱县| 靖江市| 北宁市| 屏东市| 惠安县| 汉沽区| 贵德县| 长白| 平江县| 祁东县| 隆昌县|