如何向Pandas中的DataFrame添加行
发表于:2024-10-06 作者:千家信息网编辑
千家信息网最后更新 2024年10月06日,这篇文章给大家分享的是有关如何向Pandas中的DataFrame添加行的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。您可以使用df.loc()函数在Pandas Data
千家信息网最后更新 2024年10月06日如何向Pandas中的DataFrame添加行
这篇文章给大家分享的是有关如何向Pandas中的DataFrame添加行的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
您可以使用df.loc()函数在Pandas DataFrame的末尾添加一行:
#add row to end of DataFramedf.loc[len(df.index)] = [value1, value2, value3, ...]
您可以使用df.append()函数将现有 DataFrame 的几行附加到另一个 DataFrame 的末尾:
#append rows of df2 to end of existing DataFramedf = df.append(df2, ignore_index = True)
下面的例子展示了如何在实践中使用这些函数。
示例 1:向 Pandas DataFrame 添加一行
以下代码显示了如何在 Pandas DataFrame 的末尾添加一行:
import pandas as pd #create DataFramedf = pd.DataFrame({'points': [10, 12, 12, 14, 13, 18], 'rebounds': [7, 7, 8, 13, 7, 4], 'assists': [11, 8, 10, 6, 6, 5]}) #view DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 5 #add new row to end of DataFramedf.loc[len(df.index)] = [20, 7, 5] #view updated DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 56 20 7 5
示例 2:向 Pandas DataFrame 添加几行
以下代码显示了如何将现有 DataFrame 的几行添加到另一个 DataFrame 的末尾:
import pandas as pd #create DataFramedf = pd.DataFrame({'points': [10, 12, 12, 14, 13, 18], 'rebounds': [7, 7, 8, 13, 7, 4], 'assists': [11, 8, 10, 6, 6, 5]}) #view DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 5 #define second DataFramedf2 = pd.DataFrame({'points': [21, 25, 26], 'rebounds': [7, 7, 13], 'assists': [11, 3, 3]}) #add new row to end of DataFramedf = df.append(df2, ignore_index = True) #view updated DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 56 21 7 117 25 7 38 26 13 3
请注意,两个 DataFrame 应该具有相同的列名,以便成功地将一个 DataFrame 的行附加到另一个 DataFrame 的末尾。
补充:优雅的增加一列,一定要优雅!
df['new_colu']='12'#向 DataFrame 添加一列,该列为同一值
dfOut[93]: one two three four new_colua 0 1 2 3 12b 4 5 6 7 12c 8 9 10 11 12d 12 13 14 15 12new_raw 3 3 3 3 12
感谢各位的阅读!关于"如何向Pandas中的DataFrame添加行"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
末尾
一行
函数
优雅
代码
内容
更多
示例
篇文章
加一
附加
不错
实用
相同
成功
两个
例子
文章
看吧
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
增强型服务器密码机
软件网络服务器
徐州iosapp软件开发
天津 代理服务器
服务器连接手机上网慢
数据库实验室设备管理系统功能
商城开发需要服务器
试述防火墙在网络安全中的作用
odoo 数据库文件
数据库的文件组成
微商城服务器配置
反向代理服务器DNS
银行网络安全宣传周美篇
谷安网络安全专业就业怎么样
长沙科技互联网企业
上海软件开发外包多少钱
机场网络技术人员
通化节能软件开发多少钱
c数据库多事务处理
网络安全等级保护课程大纲
现在软件开发的技术环境
服务器登陆ip
技术先行网络安全
久大网络技术公司
安卓的软件开发方案多少钱
hp380服务器光驱
宿豫区多功能网络技术参考价格
做ui有什么软件开发
智能家庭网络技术标准与应用实践
睿渥杯网络安全