个性化阅读
专注于IT技术分析

Pandas DataFrame.append()使用详解

Pandas append()函数用于将其他数据框的行添加到给定数据框的末尾, 并返回一个新的数据框对象。新列和新单元格将插入到原始DataFrame中, 并用NaN值填充。

句法:

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

参数:

  • 其他:DataFrame或类似Series / dict的对象, 或这些对象的列表
    它是指要附加的数据。
  • ignore_index:如果为true, 则不使用索引标签。
  • verify_integrity:如果为true, 则在创建具有重复项的索引时会引发ValueError。
  • sort:如果self和other的列不对齐, 则对列进行排序。默认排序已弃用, 在将来的Pandas版本中它将更改为不排序。我们通过sort = True明确地使警告和排序保持沉默, 而我们通过sort = False明确地使警告而不是排序保持沉默。

返回值:

它返回附加的DataFrame作为输出。

范例1:

import pandas as pd 
# Create first Dataframe using dictionary 
info1 = pd.DataFrame({"x":[25, 15, 12, 19], "y":[47, 24, 17, 29]})   
# Create second Dataframe using dictionary 
Info2 = pd.DataFrame({"x":[25, 15, 12], "y":[47, 24, 17], "z":[38, 12, 45]}) 
# append info2 at end in info1 
info.append(info2, ignore_index = True)

输出

x       y      z
0    25      47     NaN  
1    15      24     NaN
2    12      17     NaN
3    19      29     NaN
4    25      47     38.0
5    15      24     12.0
6    12      17     45.0

范例2:

import pandas as pd   
# Create first Dataframe using dictionary 
info1 = info = pd.DataFrame({"x":[15, 25, 37, 42], "y":[24, 38, 18, 45]})   
# Create second Dataframe using dictionary 
info2 = pd.DataFrame({"x":[15, 25, 37], "y":[24, 38, 45]})   
# print value of info1 
print(info1, "\n")  
# print values of info2 
info2 
# append info2 at the end of info1 dataframe 
info1.append(df2) 
# Continuous index value will maintained 
# across rows in the new appended data frame. 
info.append(info2, ignore_index = True)

输出

x     y
0    15   24
1    25   38
2    37   18
3    42   45
4    15   24
5    25   38
6    37   45

赞(0)
未经允许不得转载:srcmini » Pandas DataFrame.append()使用详解

评论 抢沙发

评论前必须登录!