如何在 pandas dataframe 中添加一行-ag捕鱼王app官网

如何在 pandas dataframe 中添加一行

作者:迹忆客 最近更新:2024/04/20 浏览次数:

pandas 旨在加载一个完全填充的 dataframe。我们可以在 pandas.dataframe 中一一添加。这可以通过使用各种方法来完成,例如 .loc,字典,pandas.concat()dataframe.append()


使用 .loc [index] 方法将行添加到带有列表的 pandas dataframe 中

loc[index] 会将新列表作为新行,并将其添加到 pandas.dataframe 的索引 index 中。

考虑以下代码:

# python 3.x
import pandas as pd
# list of tuples
fruit_list = [("orange", 34, "yes")]
# create a dataframe object
df = pd.dataframe(fruit_list, columns=["name", "price", "stock"])
# add new row
df.loc[1] = ["mango", 4, "no"]
df.loc[2] = ["apple", 14, "yes"]
print(df)

结果:

     name  price stock
0  orange     34   yes
1   mango      4    no
2   apple     14   yes

将字典作为行添加到 pandas dataframe

append() 可以直接将字典中的键值作为一行,将其添加到 pandas dataframe 中。

考虑以下代码:

# python 3.x
import pandas as pd
# list of tuples
fruit_list = [("orange", 34, "yes")]
# create a dataframe object
df = pd.dataframe(fruit_list, columns=["name", "price", "stock"])
# add new row
df = df.append({"name": "apple", "price": 23, "stock": "no"}, ignore_index=true)
df = df.append({"name": "mango", "price": 13, "stock": "yes"}, ignore_index=true)
print(df)

结果:

     name  price stock
0  orange     34   yes
1   apple     23    no
2   mango     13   yes

dataframe .append 方法添加一行

.append 可用于将其他 dataframe 的行追加到原始 dataframe 的末尾,并返回一个新的 dataframe。来自新 dataframe 的列(不在原始 datafarme 中)也添加到现有的 dataframe 中,新的单元格值填充为 nan

考虑以下代码:

# python 3.x
import pandas as pd
# list of tuples
fruit_list = [("orange", 34, "yes")]
# create a dataframe object
df = pd.dataframe(fruit_list, columns=["name", "price", "stock"])
print("original dataframe:")
print(df)
print(".............................")
print(".............................")
new_fruit_list = [("apple", 34, "yes", "small")]
dfnew = pd.dataframe(new_fruit_list, columns=["name", "price", "stock", "type"])
print("newly created dataframe:")
print(dfnew)
print(".............................")
print(".............................")
# append one dataframe to othher
df = df.append(dfnew, ignore_index=true)
print("copying dataframe to orignal...")
print(df)

ignore_index = true 将忽略新 dataframeindex 并从原始 dataframe 为其分配新索引。

输出:

original dataframe:
     name  price stock
0  orange     34   yes
.............................
.............................
newly created dataframe:
    name  price stock   type
0  apple     34   yes  small
.............................
.............................
copying  dataframe to original..:
     name  price stock   type
0  orange     34   yes    nan
1   apple     34   yes  small

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图