自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

再見(jiàn)Excel,你好Pandas!

大數(shù)據(jù)
寫(xiě)⼊數(shù)據(jù)時(shí),要注意不同的⽂件格式選⽤不同的⽅法,如寫(xiě)⼊csv⽂件使⽤to_csv,寫(xiě)⼊ excel時(shí)使⽤to_excel,并且要注意添加編碼⽅式。

再⻅Excel,你好Pandas!

數(shù)據(jù)的寫(xiě)⼊:

寫(xiě)⼊數(shù)據(jù)時(shí),要注意不同的⽂件格式選⽤不同的⽅法,如寫(xiě)⼊csv⽂件使⽤to_csv,寫(xiě)⼊ excel時(shí)使⽤to_excel,并且要注意添加編碼⽅式,下面創(chuàng)建⼀個(gè)表: 

  1. from pandas import Series,DataFrame   
  2. # 使用字典創(chuàng)建 
  3.  
  4. index_list['001','002','003','004','005','006','007','008','009','010' 
  5. name_list = ['李白','王昭君','諸葛亮','狄仁杰','孫尚香','妲己','周瑜','張飛','王昭君','大  
  6. 喬'] 
  7.  
  8. age_list=[25,28,27,25,30,29,25,32,28,26]  
  9. salary_list=['10k','12.5k','20k','14k','12k','17k','18k','21k','22k','21.5k' 
  10. marital_list = ['NO','NO','YES','YES','NO','NO','NO','YES','NO','YES' 
  11. dic={ 
  12.  
  13. '姓名': Series(data=name_list,index=index_list),  
  14. '年齡': Series(data=age_list,index=index_list), 
  15.  '薪資': Series(data=salary_list,index=index_list),  
  16. '婚姻狀況': Series(data=marital_list,index=index_list)  
  17.  
  18. df=DataFrame(dic)  
  19. # 寫(xiě)入csv,path_or_buf為寫(xiě)入文本文件  
  20. df.to_csv(path_or_buf='./People_Information.csv' 
  21. encoding='utf_8_sig',index=False 
  22. print('end'

這⾥調(diào)⽤to_csv⽅法 寫(xiě)⼊數(shù)據(jù),可以指定路徑,參數(shù)encoding是指定編碼⽅式,這樣遇到中⽂不易出現(xiàn)亂碼,參數(shù)index=False是為了去除掉⾏索引,不然⾏索引1,2,3,4等也會(huì)放到表⾥。

數(shù)據(jù)的讀取:

讀取數(shù)據(jù)時(shí),不同的⽂件格式使⽤的⽅法也不⼀樣, 讀取csv使⽤read_csv,excel使⽤ read_excel,并且可以指定⽂件進(jìn)⾏讀,另外⼀個(gè)Excel⽂件可以創(chuàng)建多個(gè)表,然后在不同的表中存儲(chǔ)不同數(shù)據(jù),這種形式的⽂件很常⻅。但是要注意csv⽂件不存在多個(gè)sheet的問(wèn)題。 

  1. 如: import pandas as pd  
  2. #sheet_name指定讀取⼯作鋪中的那個(gè)sheet(sheet名稱)  
  3. sheet1 = pd.read_excel('./data/sheet.xlsx',sheet_name='sheet1' 
  4. print(sheet1.head())  
  5. sheet2 = pd.read_excel('./data/sheet.xlsx',sheet_name='sheet2' 
  6. print(sheet2.head())  
  7. 當(dāng)csv或者excel中數(shù)據(jù)的第⼀⾏是⼀條臟數(shù)據(jù),可以利⽤read_excel()中的header參數(shù)進(jìn)  
  8. ⾏選擇哪⼀⾏作為我們的列索引。如:  
  9. import pandas as pd  
  10. #這里將header設(shè)置為1(第一行是0),代表數(shù)據(jù)將從第2行開(kāi)始讀取,第一行的數(shù)據(jù)會(huì)被  
  11. 忽略  
  12. people = pd.read_csv('./data/People1.csv',header = 1)  
  13. print(people.head())  

如果都不滿⾜的你的要求,可以將header設(shè)置為None,列索引值會(huì)使⽤默認(rèn)的1、2、 3、4,之后在⾃⾏設(shè)置。

當(dāng)指定了header的值,讀出來(lái)的數(shù)據(jù)就是從該⾏開(kāi)始向下切⽚,該⾏以上的數(shù)據(jù)會(huì)被忽略。

 

責(zé)任編輯:未麗燕 來(lái)源: 今日頭條
相關(guān)推薦

2020-09-27 11:15:37

可視化PandasPython

2012-08-01 09:50:09

HotmailOutlook微軟

2015-03-16 11:09:28

MongoDBPostgreSQL數(shù)據(jù)遷移

2021-04-23 09:09:19

GraphQLREST查詢

2019-02-01 10:35:33

PythonGo語(yǔ)言編程語(yǔ)言

2021-07-27 05:56:53

CrocFTPSFTP

2018-01-02 08:40:19

云安全云遷移數(shù)據(jù)泄露

2023-11-28 17:24:45

2009-03-30 08:44:22

微軟Windows 7操作系統(tǒng)

2011-01-07 18:05:37

QQ騰訊移動(dòng)互聯(lián)網(wǎng)

2021-06-02 22:25:26

2G5G運(yùn)營(yíng)商

2014-11-27 14:26:46

蘋果iPhone停產(chǎn)

2021-02-17 13:20:51

forpandas語(yǔ)言

2022-08-24 13:39:46

PandasGUIExcel

2011-09-23 14:24:58

惠普云計(jì)算李艾科

2023-01-05 11:34:46

PandasMerge方法

2021-04-15 22:17:16

PythonExcel代碼

2020-10-29 10:44:59

斗魚(yú)騰訊虎牙

2021-01-21 07:16:03

RocketMQKafka中間件

2022-09-26 00:00:02

PandasExcel文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)