Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use append function in pandas

Shulou Source: shulou.com Published: 2022-06-01 03:01:00 10月04日 Update

This article mainly introduces how to use the append function in pandas, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Append

Append is mainly used to append data, which is a relatively simple and direct way of data merging.

Df.append (other, ignore_index: 'bool' = False, verify_integrity:' bool' = False, sort: 'bool' = False,)->' DataFrame'

In the function method, the meanings of the parameters are as follows:

Other: data for append, which can be DataFrame or Series or a list of components

Ignore_index: whether to keep the original index

Verify_integrity: check whether the index is duplicated. If it is True, an error will be reported if the index is duplicated.

Sort: sort columns in union and merge mode

Next, we will demonstrate the function of this function.

Basic addition

In [41]: df1.append (df2) Out [41]: letter number0 a 11 b 20 c 31 d 4In [42]: df1.append ([df1,df2] Df3]) Out [42]: letter number animal0 a 1 NaN1 b 2 NaN0 a 1 NaN1 b 2 NaN0 c 3 NaN1 d 4 NaN0 c 3 cat1 d 4 dog

Columns reset (do not retain the original index)

In [43]: df1.append ([df1,df2,df3], ignore_index=True) Out [43]: letter number animal0 a 1 NaN1 b 2 NaN2 a 1 NaN3 b 2 NaN4 c 3 NaN5 d 4 NaN6 c 3 cat7 d 4 dog

Detect duplicates

If the index is duplicated, it fails the test and an error is reported.

In [44]: df1.append ([df1,df2], verify_integrity=True) Traceback (most recent call last):... ValueError: Indexes have overlapping values: Int64Index ([0,1], dtype='int64')

Index sort

In [46]: df1.append ([df1,df2,df3], sort=True) Out [46]: animal letter number0 NaN a 11 NaN b 20 NaN a 11 NaN b 20 NaN c 31 NaN d 40 cat c 31 dog d 4

Append Series

In [49]: s = pd.Series ({'letter':'s1','number':9}) In [50]: sOut [50]: letter s1number 9dtype: objectIn [51]: df1.append (s) Traceback (most recent call last):... TypeError: Can only append a Series if ignore_index=True or if the Series has a nameIn [53]: df1.append (s, ignore_index=True) Out [53]: letter number0 a 11 b 22 S19

Additional dictionary

This works better when crawling. Every time a piece of data is crawled, it is merged into DataFrame-like data and stored.

In [54]: dic = {'letter':'s1','number':9} In [55]: df1.append (dic, ignore_index=True) Out [55]: letter number0 a 11 b 22 s19 Thank you for reading this article carefully. I hope the article "how to use append function in pandas" shared by the editor will be helpful to you. At the same time, I hope you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Index data function article test way report sort next value interest function parameter at the same time meaning foundation good use dictionary method time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Docker vpn Xiaomi