最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

import pandas as pd

# 創(chuàng)建一個(gè)數(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)建一個(gè)包含大數(shù)據(jù)公司行號的列表
comp_rows = [1, 2, 3]

# 創(chuàng)建一個(gè)空數(shù)據(jù)框來存儲(chǔ)過濾后的公司信息
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)

# 打印錯(cuò)誤信息
print(bigdata_comp)

錯(cuò)誤:

---------------------------------------------------------------------------
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() 方法來連接兩個(gè)數(shù)據(jù)框。

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

# 將列表轉(zhuǎn)換為數(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 ...

到此這篇關(guān)于Pandas添加行至現(xiàn)有數(shù)據(jù)框的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Pandas添加行至現(xiàn)有數(shù)據(jù)框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python 日期區(qū)間處理 (本周本月上周上月...)

    Python 日期區(qū)間處理 (本周本月上周上月...)

    這篇文章主要介紹了Python 日期區(qū)間處理 (本周本月上周上月...),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 關(guān)于如何把Python對象存儲(chǔ)為文件的方法詳解

    關(guān)于如何把Python對象存儲(chǔ)為文件的方法詳解

    本文將給大家介紹如何把Python對象存儲(chǔ)為文件的方法,pickle可以用二進(jìn)制表示并讀寫python數(shù)據(jù),這個(gè)功能并不安全,如果把一個(gè)pickle暴露給別人,有被植入惡意程序的風(fēng)險(xiǎn),文中通過代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Python結(jié)合wxPython打造一個(gè)優(yōu)雅的圖片預(yù)覽工具

    Python結(jié)合wxPython打造一個(gè)優(yōu)雅的圖片預(yù)覽工具

    在日常工作中,我們經(jīng)常需要快速預(yù)覽圖片文件或剪貼板中的圖片,今天,小編將帶大家用 wxPython 開發(fā)一個(gè)簡潔實(shí)用的圖片預(yù)覽工具,感興趣的小伙伴可以了解下
    2025-12-12
  • 一文詳解如何在Python中進(jìn)行數(shù)學(xué)建模

    一文詳解如何在Python中進(jìn)行數(shù)學(xué)建模

    數(shù)學(xué)建模是數(shù)據(jù)科學(xué)中使用的強(qiáng)大工具,通過數(shù)學(xué)方程和算法來表示真實(shí)世界的系統(tǒng)和現(xiàn)象,本文將指導(dǎo)大家完成Python中的數(shù)學(xué)建模過程,感興趣的可以了解下
    2024-11-11
  • python接口自動(dòng)化之使用token傳入到header消息頭中

    python接口自動(dòng)化之使用token傳入到header消息頭中

    這篇文章主要介紹了python接口自動(dòng)化之使用token傳入到header消息頭中問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 淺談Python中函數(shù)的定義及其調(diào)用方法

    淺談Python中函數(shù)的定義及其調(diào)用方法

    今天小編就為大家分享一篇淺談Python中函數(shù)的定義及其調(diào)用方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python xlwt模塊的使用解析

    python xlwt模塊的使用解析

    這篇文章主要介紹了python xlwt模塊的使用解析,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • python中?OpenCV和Pillow處理圖像操作及時(shí)間對比

    python中?OpenCV和Pillow處理圖像操作及時(shí)間對比

    這篇文章主要介紹了python中OpenCV和Pillow處理圖像操作及時(shí)間對比,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • python 獲取指定文件夾下所有文件名稱并寫入列表的實(shí)例

    python 獲取指定文件夾下所有文件名稱并寫入列表的實(shí)例

    下面小編就為大家分享一篇python 獲取指定文件夾下所有文件名稱并寫入列表的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python 基于opencv去除圖片陰影

    python 基于opencv去除圖片陰影

    這篇文章主要介紹了python 基于opencv去除圖片陰影的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01

最新評論

府谷县| 关岭| 吴江市| 平泉县| 灯塔市| 来凤县| 应用必备| 盐源县| 达州市| 瑞金市| 海盐县| 铜川市| 称多县| 通渭县| 维西| 金华市| 泊头市| 娄底市| 随州市| 陈巴尔虎旗| 肥西县| 怀来县| 宁远县| 宜宾市| 射洪县| 泰来县| 长垣县| 弥勒县| 永川市| 姚安县| 庐江县| 江西省| 万山特区| 历史| 铜陵市| 罗定市| 潼关县| 西贡区| 吉林省| 阜康市| 余庆县|