Pandas添加行至現(xiàn)有數(shù)據(jù)框的實(shí)現(xiàn)示例
在學(xué)習(xí)數(shù)據(jù)科學(xué)時(shí),想要根據(jù)一個(gè)列表中包含的不同行業(yè)公司的行號(hào),從一個(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ù)公司行號(hào)的列表 comp_rows = [1, 2, 3] # 創(chuàng)建一個(gè)空數(shù)據(jù)框來(lái)存儲(chǔ)過(guò)濾后的公司信息 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() 方法來(lái)選擇特定行,然后將其添加到新的數(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() 方法來(lái)連接兩個(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中發(fā)布Web應(yīng)用的常見(jiàn)方法與對(duì)比
這篇文章主要為大家詳細(xì)介紹了Python中發(fā)布?Web?應(yīng)用的幾種常見(jiàn)方法的實(shí)現(xiàn)詳細(xì)步驟以及它們的部署方法對(duì)比,有需要的小伙伴可以參考下2025-02-02Python 機(jī)器學(xué)習(xí)庫(kù) NumPy入門(mén)教程
在我們使用Python語(yǔ)言進(jìn)行機(jī)器學(xué)習(xí)編程的時(shí)候,這是一個(gè)非常常用的基礎(chǔ)庫(kù)。本文針對(duì)Python 機(jī)器學(xué)習(xí)庫(kù) NumPy入門(mén)教程,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04Python機(jī)器學(xué)習(xí)之基礎(chǔ)概述
今天帶大家回顧python機(jī)器學(xué)習(xí)的相關(guān)知識(shí),文中非常詳細(xì)的介紹了Python機(jī)器學(xué)習(xí)的基礎(chǔ)概述,算法分類(lèi)及研究?jī)?nèi)容,需要的朋友可以參考下2021-05-05Python數(shù)據(jù)分析庫(kù)pandas基本操作方法
下面小編就為大家分享一篇Python數(shù)據(jù)分析庫(kù)pandas基本操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼
本文主要介紹了Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03