亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Pandas添加行至現(xiàn)有數(shù)據(jù)框的實現(xiàn)示例

 更新時間:2025年04月23日 10:20:48   作者:qq^^614136809  
本文主要介紹了Pandas添加行至現(xiàn)有數(shù)據(jù)框的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

在學習數(shù)據(jù)科學時,想要根據(jù)一個列表中包含的不同行業(yè)公司的行號,從一個大數(shù)據(jù)公司列表中提取信息,并創(chuàng)建一個新的數(shù)據(jù)框。在嘗試添加行到現(xiàn)有數(shù)據(jù)框時遇到了錯誤。

import pandas as pd

# 創(chuàng)建一個數(shù)據(jù)框
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...']
})

# 創(chuàng)建一個包含大數(shù)據(jù)公司行號的列表
comp_rows = [1, 2, 3]

# 創(chuàng)建一個空數(shù)據(jù)框來存儲過濾后的公司信息
bigdata_comp = pd.DataFrame(data=None,columns=['company_url','company','tag_line','product','data'])

# 嘗試添加行到現(xiàn)有數(shù)據(jù)框
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() 方法來選擇特定行,然后將其添加到新的數(shù)據(jù)框中。

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

# 添加行到新的數(shù)據(jù)框中
bigdata_comp = pd.concat([bigdata_comp, filtered_data], ignore_index=True)

# 打印新的數(shù)據(jù)框
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() 方法來連接兩個數(shù)據(jù)框。

# 創(chuàng)建一個包含大數(shù)據(jù)公司信息的列表
bigdata_list = []
for number in comp_rows:
    bigdata_list.append(data.iloc[number])

# 將列表轉換為數(shù)據(jù)框
bigdata_comp = pd.concat(bigdata_list, ignore_index=True)

# 打印新的數(shù)據(jù)框
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添加行至現(xiàn)有數(shù)據(jù)框的實現(xiàn)示例的文章就介紹到這了,更多相關Pandas添加行至現(xiàn)有數(shù)據(jù)框內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python API 操作Hadoop hdfs詳解

    Python API 操作Hadoop hdfs詳解

    這篇文章主要介紹了Python API 操作Hadoop hdfs詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python中發(fā)布Web應用的常見方法與對比

    Python中發(fā)布Web應用的常見方法與對比

    這篇文章主要為大家詳細介紹了Python中發(fā)布?Web?應用的幾種常見方法的實現(xiàn)詳細步驟以及它們的部署方法對比,有需要的小伙伴可以參考下
    2025-02-02
  • Python 機器學習庫 NumPy入門教程

    Python 機器學習庫 NumPy入門教程

    在我們使用Python語言進行機器學習編程的時候,這是一個非常常用的基礎庫。本文針對Python 機器學習庫 NumPy入門教程,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-04-04
  • python 實現(xiàn)兔子生兔子示例

    python 實現(xiàn)兔子生兔子示例

    今天小編就為大家分享一篇python 實現(xiàn)兔子生兔子示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python機器學習之基礎概述

    Python機器學習之基礎概述

    今天帶大家回顧python機器學習的相關知識,文中非常詳細的介紹了Python機器學習的基礎概述,算法分類及研究內容,需要的朋友可以參考下
    2021-05-05
  • Python數(shù)據(jù)分析庫pandas基本操作方法

    Python數(shù)據(jù)分析庫pandas基本操作方法

    下面小編就為大家分享一篇Python數(shù)據(jù)分析庫pandas基本操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python psutil庫安裝教程

    python psutil庫安裝教程

    這篇文章給大家介紹了python psutil庫安裝教程,首先要確認本機已安裝python環(huán)境,具體安裝過程大家參考下本文
    2018-03-03
  • windows下python安裝小白入門教程

    windows下python安裝小白入門教程

    這篇文章主要為大家詳細介紹了windows下python安裝小白入門教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • 利用Python的Django框架生成PDF文件的教程

    利用Python的Django框架生成PDF文件的教程

    這篇文章主要介紹了利用Python的Django框架生成PDF文件的教程,用ReportLab API動態(tài)生成PDF文件,需要的朋友可以參考下
    2015-07-07
  • Python使用Qt5實現(xiàn)水平導航欄的示例代碼

    Python使用Qt5實現(xiàn)水平導航欄的示例代碼

    本文主要介紹了Python使用Qt5實現(xiàn)水平導航欄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03

最新評論