Pandas添加行至现有数据框的实现示例

 更新时间:2025年04月23日 10:20:48   作者:qq^^614136809  
本文主要介绍了Pandas添加行至现有数据框的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在学习数据科学时,想要根据一个列表中包含的不同行业公司的行号,从一个大数据公司列表中提取信息,并创建一个新的数据框。在尝试添加行到现有数据框时遇到了错误。

import pandas as pd

# 创建一个数据框
data = pd.DataFrame({
    'company_url': ['https://angel.co/billguard', 'https://angel.co/tradesparq', 'https://angel.co/sidewalk', 'https://angel.co/pangia', 'https://angel.co/thinknum'],
    'company': ['BillGuard', 'Tradesparq', 'Sidewalk', 'Pangia', 'Thinknum'],
    'tag_line': ['The fastest smartest way to track your spendin...', 'The world''s largest social network for global ...', 'Hoovers (D&B) for the social era', 'The Internet of Things Platform: Big data mana...', 'Financial Data Analysis Thinknum is a powerful web platform to value c...'],
    'product': ['BillGuard is a personal finance security app t...', 'Tradesparq is Alibaba.com meets LinkedIn. Trad...', 'Sidewalk helps companies close more sales to s...', 'We collect and manage data from sensors embedd...', 'Thinknum is a powerful web platform to value c...'],
    'data': ['New York City · Financial Services · Security ...', 'Shanghai · B2B · Marketplaces · Big Data · Soc...', 'New York City · Lead Generation · Big Data · S...', 'San Francisco · SaaS · Clean Technology · Big ...', 'New York City · Enterprise Software · Financia...']
})

# 创建一个包含大数据公司行号的列表
comp_rows = [1, 2, 3]

# 创建一个空数据框来存储过滤后的公司信息
bigdata_comp = pd.DataFrame(data=None,columns=['company_url','company','tag_line','product','data'])

# 尝试添加行到现有数据框
for count, item in enumerate(data.iterrows()):
    for number in comp_rows:
        if int(count) == int(number):
            bigdata_comp.append(item)

# 打印错误信息
print(bigdata_comp)

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-234-1e4ea9bd9faa> in <module>()
      4     for number in comp_rows:
      5         if int(count) == int(number):
----> 6             bigdata_comp.append(item)
      7 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.pyc in append(self, other, ignore_index, verify_integrity)
   3814         from pandas.tools.merge import concat
   3815         if isinstance(other, (list, tuple)):
-> 3816             to_concat = [self] + other
   3817         else:
   3818             to_concat = [self, other]

TypeError: can only concatenate list (not "tuple") to list

解决方案

方法1:使用 .loc() 方法

可以使用 .loc() 方法来选择特定行,然后将其添加到新的数据框中。

# 使用 .loc() 方法选择特定行
filtered_data = data.loc[comp_rows]

# 添加行到新的数据框中
bigdata_comp = pd.concat([bigdata_comp, filtered_data], ignore_index=True)

# 打印新的数据框
print(bigdata_comp)

输出:

   company_url             company                tag_line                                                              product                                                                        data
0   https://angel.co/tradesparq  Tradesparq  The world''s largest social network for global ...  Tradesparq is Alibaba.com meets LinkedIn. Trad...  Shanghai · B2B · Marketplaces · Big Data · Soc...
1   https://angel.co/sidewalk   Sidewalk    Hoovers (D&B) for the social era              Sidewalk helps companies close more sales to s...  New York City · Lead Generation · Big Data · S...
2   https://angel.co/pangia  Pangia  The Internet of Things Platform: Big data mana...  We collect and manage data from sensors embedd...  San Francisco · SaaS · Clean Technology · Big ...

方法2:使用 pd.concat() 方法

也可以使用 pd.concat() 方法来连接两个数据框。

# 创建一个包含大数据公司信息的列表
bigdata_list = []
for number in comp_rows:
    bigdata_list.append(data.iloc[number])

# 将列表转换为数据框
bigdata_comp = pd.concat(bigdata_list, ignore_index=True)

# 打印新的数据框
print(bigdata_comp)

输出:

   company_url       company                tag_line                                                                      product                                                                        data
0   https://angel.co/tradesparq  Tradesparq  The world''s largest social network for global ...  Tradesparq is Alibaba.com meets LinkedIn. Trad...  Shanghai · B2B · Marketplaces · Big Data · Soc...
1   https://angel.co/sidewalk   Sidewalk    Hoovers (D&B) for the social era               Sidewalk helps companies close more sales to s...  New York City · Lead Generation · Big Data · S...
2   https://angel.co/pangia  Pangia  The Internet of Things Platform: Big data mana...  We collect and manage data from sensors embedd...  San Francisco · SaaS · Clean Technology · Big ...

到此这篇关于Pandas添加行至现有数据框的实现示例的文章就介绍到这了,更多相关Pandas添加行至现有数据框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 日期区间处理 (本周本月上周上月...)

    Python 日期区间处理 (本周本月上周上月...)

    这篇文章主要介绍了Python 日期区间处理 (本周本月上周上月...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • 关于如何把Python对象存储为文件的方法详解

    关于如何把Python对象存储为文件的方法详解

    本文将给大家介绍如何把Python对象存储为文件的方法,pickle可以用二进制表示并读写python数据,这个功能并不安全,如果把一个pickle暴露给别人,有被植入恶意程序的风险,文中通过代码给大家讲解的非常详细,需要的朋友可以参考下
    2024-01-01
  • Python结合wxPython打造一个优雅的图片预览工具

    Python结合wxPython打造一个优雅的图片预览工具

    在日常工作中,我们经常需要快速预览图片文件或剪贴板中的图片,今天,小编将带大家用 wxPython 开发一个简洁实用的图片预览工具,感兴趣的小伙伴可以了解下
    2025-12-12
  • 一文详解如何在Python中进行数学建模

    一文详解如何在Python中进行数学建模

    数学建模是数据科学中使用的强大工具,通过数学方程和算法来表示真实世界的系统和现象,本文将指导大家完成Python中的数学建模过程,感兴趣的可以了解下
    2024-11-11
  • python接口自动化之使用token传入到header消息头中

    python接口自动化之使用token传入到header消息头中

    这篇文章主要介绍了python接口自动化之使用token传入到header消息头中问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 浅谈Python中函数的定义及其调用方法

    浅谈Python中函数的定义及其调用方法

    今天小编就为大家分享一篇浅谈Python中函数的定义及其调用方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • python xlwt模块的使用解析

    python xlwt模块的使用解析

    这篇文章主要介绍了python xlwt模块的使用解析,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04
  • python中 OpenCV和Pillow处理图像操作及时间对比

    python中 OpenCV和Pillow处理图像操作及时间对比

    这篇文章主要介绍了python中OpenCV和Pillow处理图像操作及时间对比,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • python 获取指定文件夹下所有文件名称并写入列表的实例

    python 获取指定文件夹下所有文件名称并写入列表的实例

    下面小编就为大家分享一篇python 获取指定文件夹下所有文件名称并写入列表的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python 基于opencv去除图片阴影

    python 基于opencv去除图片阴影

    这篇文章主要介绍了python 基于opencv去除图片阴影的方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-01-01

最新评论