如何在 pandas dataframe 中添加一行
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
将忽略新 dataframe
的 index
并从原始 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 dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串