Pandas 教程-Pandas DataFrame.append()
Pandas append() 函数用于将其他 dataframe 的行添加到给定 dataframe 的末尾,返回一个新的 dataframe 对象。新列和新单元格被插入到原始 DataFrame 中,这些单元格填充了 NaN 值。
语法:
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)参数:
other: DataFrame 或类似字典的 Series 对象,或者它们的列表。它是要追加的数据。
ignore_index: 如果为 True,则不使用索引标签。
verify_integrity: 如果为 True,则在创建具有重复索引的索引时引发 ValueError。
sort: 如果 self 和 other 的列未对齐,则对列进行排序。默认排序已弃用,并且将在将来的 pandas 版本中更改为不排序。我们通过 sort=True 明确传递以消除警告和排序,而我们通过 sort=False 明确传递以消除警告而不进行排序。
返回值:
将追加的 DataFrame 作为输出返回。
专属福利 👉点击领取:最全Python资料合集
示例1:
import pandas as pd# Create first Dataframe using dictionaryinfo1 = pd.DataFrame({"x":[25,15,12,19],"y":[47, 24, 17, 29]})# Create second Dataframe using dictionaryInfo2 = pd.DataFrame({"x":[25, 15, 12],"y":[47, 24, 17],"z":[38, 12, 45]})# append info2 at end in info1info.append(info2, ignore_index = True)
x y z0 25 47 NaN1 15 24 NaN2 12 17 NaN3 19 29 NaN4 25 47 38.05 15 24 12.06 12 17 45.0
示例2:
import pandas as pd# Create first Dataframe using dictionaryinfo1 = info = pd.DataFrame({"x":[15, 25, 37, 42],"y":[24, 38, 18, 45]})# Create second Dataframe using dictionaryinfo2 = pd.DataFrame({"x":[15, 25, 37],"y":[24, 38, 45]})# print value of info1print(info1, "\n")# print values of info2info2# append info2 at the end of info1 dataframeinfo1.append(df2)# Continuous index value will maintained# across rows in the new appended data frame.info.append(info2, ignore_index = True)
x y0 15 241 25 382 37 183 42 454 15 245 25 386 37 45
热门推荐