程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Summary of recent common problems of pandas (I)

編輯:Python

Studying recently 《 utilize python Data analysis 》, In the actual process and use of the case pandas There are still many unsolved problems in the library , Here's a summary .

Catalog

  • One 、SettingwithCopyWarning problem
  • Two 、 Read to :: For the separator csv Warning on file
  • 3、 ... and 、 take DataFrame write in excel
  • Four 、dataframe Transposition

One 、SettingwithCopyWarning problem


As shown in the figure above , The warning shown above appears , Here's an article :https://www.jianshu.com/p/72274ccb647a
This article explains the reasons and solutions in detail , This is a chain index problem .

Two 、 Read to :: For the separator csv Warning on file


The general meaning of this warning is to tell you : because “c” The engine does not support regular expression delimiters ( Separator >1 Characters and different from “\s+” The delimiter of is interpreted as a regular expression ), You can specify engine=”python“ This parameter is used to avoid this warning . The details can be copied to the translation software . because Python The bottom layer of the language is C The realization of language , It can be simply understood as Python Yes, it is C Write out the . This needs to be explained by the boss , I don't quite understand the meaning . Change the code here to :

df1=pd.read_csv("movies.dat",sep='::',header=None,engine="python",names=mnames)

That's it , This is also 《 utilize python Data analysis 》 An example in the book .

3、 ... and 、 take DataFrame write in excel

take dataframe write in excel It can be used pandas Of to_excel(path,sheet1)path Is the path ,sheet1 Is the table name . But if you want to put more dataframe Write the same excel file , Need to use ExcelWriter() Method

import pandas as pd
# First create two dataframe
frame=pd.DataFrame({
"data1":random.randn(5),
"data2":random.randn(5),
"key1":["a","a","b","b","a"],
"key2":["one","two","one","two","one"]})
frame2=frame.copy()
writer=pd.ExcelWriter("test.xls")
frame.to_excel(writer,"frame")
frame2.to_excel(writer,"frame2")
writer.save()

Here are two dataframe, Write it to text file , The names of the two tables are named frame and frame2. Pay attention to the final execution writer.save() Code , Otherwise, the file will not be created , Only in cache .

If you want to put multiple dataframe Write the same table , You can use startcol and startrow These two parameters are used to implement

writer=pd.ExcelWriter("test.xls")
frame.to_excel(writer,startrow=2)
frame2.to_excel(writer,startrow=18,startcol=18)
writer.save()
#startrow Represents the starting line written ,startcol Represents the starting column written 

Four 、dataframe Transposition

dataframe There's something like this numpy Array similar transpose operation .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved