When studying data science, you want to extract information from a list of big data companies based on the line numbers of companies in different industries contained in a list and create a new data frame. An error was encountered while trying to add rows to an existing dataframe.
import pandas as pd # Create a data framedata = ({ 'company_url': ['/billguard', '/tradesparq', '/sidewalk', '/pangia', '/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 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...'] }) # Create a list of big data company line numberscomp_rows = [1, 2, 3] # Create an empty data frame to store filtered company informationbigdata_comp = (data=None,columns=['company_url','company','tag_line','product','data']) # Try adding rows to existing dataframefor count, item in enumerate(()): for number in comp_rows: if int(count) == int(number): bigdata_comp.append(item) # Print error messageprint(bigdata_comp)
mistake:
--------------------------------------------------------------------------- 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//Versions/2.7/lib/python2.7/site-packages/pandas/core/ in append(self, other, ignore_index, verify_integrity) 3814 from 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
Solution
Method 1: Use the .loc() method
You can use the .loc() method to select a specific row and then add it to a new dataframe.
# Use the .loc() method to select a specific rowfiltered_data = [comp_rows] # Add rows to new dataframebigdata_comp = ([bigdata_comp, filtered_data], ignore_index=True) # Print a new data frameprint(bigdata_comp)
Output:
company_url company tag_line product data
0 /tradesparq Tradesparq The world''s largest social network for global ... Tradesparq is meets LinkedIn. Trad... Shanghai · B2B · Marketplaces · Big Data · Soc...
1 /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 /pangia Pangia The Internet of Things Platform: Big data mana... We collect and manage data from sensors embedd... San Francisco · SaaS · Clean Technology · Big ...
Method 2: Use the () method
You can also use the () method to connect two data frames.
# Create a list of big data company informationbigdata_list = [] for number in comp_rows: bigdata_list.append([number]) # Convert list to data framebigdata_comp = (bigdata_list, ignore_index=True) # Print a new data frameprint(bigdata_comp)
Output:
company_url company tag_line product data
0 /tradesparq Tradesparq The world''s largest social network for global ... Tradesparq is meets LinkedIn. Trad... Shanghai · B2B · Marketplaces · Big Data · Soc...
1 /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 /pangia Pangia The Internet of Things Platform: Big data mana... We collect and manage data from sensors embedd... San Francisco · SaaS · Clean Technology · Big ...
This is the article about the implementation example of Pandas adding rows to existing data frames. For more information about Pandas adding rows to existing data frames, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!