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

Pandas knowledge points - add operation append

編輯:Python

stay Pandas in ,append() Method is used to convert one or more DataFrame or Series Add to DataFrame in .append() Method can also be used for merge operations , In this paper, append() Method usage .

One append() To achieve a merger


append(other): Put one or more DataFrame Add to call append() Of DataFrame in , Realize the function of merging ,other Parameters are passed into the merged DataFrame, If you need to add more than one DataFrame, Then pass in a list or tuple .

append() Method realizes the function of merging by adding , This merge function is by line ( The longitudinal ) To merge , The number of rows of the merged result is all DataFrame Sum of rows .

Two Fill in nonexistent columns


If the append() Of DataFrame And incoming append() Of DataFrame There are different columns in , After adding, null values will be filled in the nonexistent Columns , So even if two DataFrame Having different columns does not affect the addition operation .

3、 ... and Add multiple DataFrame


Add multiple DataFrame when , Pass in multiple... As a list or tuple DataFrame that will do , The added principle remains the same . if necessary , You can batch DataFrame Merge into one DataFrame.

Four Reset row index


ignore_index: ignore_index The parameter defaults to False, The row index of the result remains the original DataFrame Row index in , Even if the same row index exists, it is not affected . take ignore_index It is amended as follows True, The row index of the result is reset from 0 Integer index to begin with .

verify_integrity: verify_integrity The parameter defaults to False, Added DataFrame When there are the same row indexes in , You can keep the original results . take verify_integrity It is amended as follows True, If you add DataFrame There are the same row indexes in , Will throw out ValueError. Set up verify_integrity Parameter is True, To avoid duplicate row indexes in the results , But it is likely to cause the addition to fail , Therefore, we need to first observe whether the original data is suitable for .

ignore_index and verify_integrity When used at the same time ,ignore_index Sir , So when two parameters are used at the same time , Does not throw an exception .

5、 ... and add to Series


append() Methods can also be used in DataFrame Add Series. add to Series when , To put ignore_index Parameter set to True Or to Series Set up name Parameters , Otherwise it will throw TypeError, as a result of Series No names .

Set up ignore_index Parameter is True Will reset the row index of the result , It's added like this Series As a line in the result , The row index will be generated automatically .

Appoint Series Of name Parameters , such Series Will be with name Parameters are added as row indexes to DataFrame in . Even if the designated name Value and DataFrame Duplicate row index in , You can also successfully add (verify_integrity Not for True).

6、 ... and summary

As of this article , This series introduces five methods that can be used for merge operations :concat()、merge()、join()、combine()、append(), Summarize their usage differences .

concat(): Connection operation , Can connect multiple DataFrame, You can set whether to merge by row or column . Yes inner、outer、left、right Four different connection modes . You can set the index of the result , In particular, there are many ways to deal with multiple row indexes .

merge(): Merge operation , Can only be used to merge two DataFrame, And they are merged by column , Only when two DataFrame The effect of merging by row is only when the column names of are exactly the same . Merge according to the specified join column ( Or row index ) And connection mode to match two DataFrame The line of . You can set the suffix of the same column name and display whether the connected column is in two columns in the result DataFrame There's always .

join(): Join operation , Can be in a DataFrame Add more than one DataFrame, The results are merged by column . Merge according to the specified join column ( Or row index ) And connection mode to match two DataFrame The line of , You can also set the suffix of the same column name , So sometimes join() and merge() They can switch to each other .

combine(): Joint operation , For two DataFrame, Join by column . Joint operation is to put a DataFrame Part of the data in the uses another DataFrame Replace or supplement the data in , A function is used to define the rules for fetching data during Union . Null values can also be populated during Union .

append(): Add operation , Multiple DataFrame Add to a DataFrame in , Add by line . The add operation simply adds multiple DataFrame Splice together in rows , You can reset the row index .


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