将多个列表转换为dataframe python
我想把列表放在数据框中,我的代码是将多个列表转换为dataframe python,python,list,pandas,Python,List,Pandas,我想把列表放在数据框中,我的代码是 webpage_urls = ["https://data.gov.au/dataset?q=&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&organization=departmentofagriculturefisheriesandforestry&am
webpage_urls = ["https://data.gov.au/dataset?q=&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&organization=departmentofagriculturefisheriesandforestry&_groups_limit=0",
"https://data.gov.au/dataset?q=&organization=commonwealthscientificandindustrialresearchorganisation&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&_groups_limit=0",
"https://data.gov.au/dataset?q=&organization=bureauofmeteorology&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&_groups_limit=0",
"https://data.gov.au/dataset?q=&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&organization=tasmanianmuseumandartgallery&_groups_limit=0",
"https://data.gov.au/dataset?q=&organization=department-of-industry&sort=extras_harvest_portal+asc%2C+score+desc%2C+metadata_modified+desc&_organization_limit=0&groups=sciences&_groups_limit=0"]
for i in webpage_urls:
wiki2 = i
page= urllib.request.urlopen(wiki2)
soup = BeautifulSoup(page)
# fetching organisations
data3 = soup.find_all('li', class_="nav-item active")
lobbying1 = []
for element in data3:
lobbying1.append(element.span.get_text())
print(lobbying1)
df = pd.DataFrame({'Organisation':lobbying1})
我上面的代码给出的输出为:
['Reserve Bank of Aus... (24)', 'Business Support an... (24)']
['Department of Finance (16)', 'Business Support an... (16)']
['Department of Agric... (13)', 'Business Support an... (13)']...so on
这是多个列表,不是嵌套列表,我得到的数据框如下所示:
Organisation
0 Australian Charitie... (1)
1 Business Support an... (1)
我希望输出为两列,第一列为列表的第一个元素,第二列为列表的第二个元素,我希望所有条目:
Organisation Groups
Australian Cha... Business Support and...
帮我一把。你的游说名单是一个名单。因此,只需调用
pd.dataframe
即可获得两列数据框,如下所示:
lobbying1 = [['Reserve Bank of Aus... (24)', 'Business Support an... (24)'],
['Department of Finance (16)', 'Business Support an... (16)'],
['Department of Agric... (13)', 'Business Support an... (13)']]
df = pd.DataFrame(main_list, columns=['Organization','Groups'])
您可以将其作为输出
>>> df.head()
Organization Groups
0 Reserve Bank of Aus... (24) Business Support an... (24)
1 Department of Finance (16) Business Support an... (16)
2 Department of Agric... (13) Business Support an... (13)
>>>
您的列表
游说1
是一个列表列表。因此,只需调用pd.dataframe
即可获得两列数据框,如下所示:
lobbying1 = [['Reserve Bank of Aus... (24)', 'Business Support an... (24)'],
['Department of Finance (16)', 'Business Support an... (16)'],
['Department of Agric... (13)', 'Business Support an... (13)']]
df = pd.DataFrame(main_list, columns=['Organization','Groups'])
您可以将其作为输出
>>> df.head()
Organization Groups
0 Reserve Bank of Aus... (24) Business Support an... (24)
1 Department of Finance (16) Business Support an... (16)
2 Department of Agric... (13) Business Support an... (13)
>>>
我认为您需要为列表列表添加
[]
,然后使用构造函数:
df = pd.DataFrame([lobbying1], columns=['Organization','Groups'])
print (df)
Organization Groups
0 Department of Agric... (35) Science (35)
Organization Groups
0 Commonwealth Scient... (8) Science (8)
Organization Groups
0 Bureau of Meteorology (4) Science (4)
Organization Groups
0 Tasmanian Museum an... (1) Science (1)
Organization Groups
0 Department of Indus... (1) Science (1)
如果所有数据都需要一个DataFrame
,请将lobbying1
附加到data
列表中,然后调用构造函数:
data = []
for i in webpage_urls:
wiki2 = i
page= urllib.request.urlopen(wiki2)
soup = BeautifulSoup(page)
# fetching organisations
data3 = soup.find_all('li', class_="nav-item active")
lobbying1 = []
for element in data3:
lobbying1.append(element.span.get_text())
data.append(lobbying1)
df = pd.DataFrame(data, columns=['Organization','Groups'])
print (df)
Organization Groups
0 Department of Agric... (35) Science (35)
1 Commonwealth Scient... (8) Science (8)
2 Bureau of Meteorology (4) Science (4)
3 Tasmanian Museum an... (1) Science (1)
4 Department of Indus... (1) Science (1)
我认为您需要为列表列表添加
[]
,然后使用构造函数:
df = pd.DataFrame([lobbying1], columns=['Organization','Groups'])
print (df)
Organization Groups
0 Department of Agric... (35) Science (35)
Organization Groups
0 Commonwealth Scient... (8) Science (8)
Organization Groups
0 Bureau of Meteorology (4) Science (4)
Organization Groups
0 Tasmanian Museum an... (1) Science (1)
Organization Groups
0 Department of Indus... (1) Science (1)
如果所有数据都需要一个DataFrame
,请将lobbying1
附加到data
列表中,然后调用构造函数:
data = []
for i in webpage_urls:
wiki2 = i
page= urllib.request.urlopen(wiki2)
soup = BeautifulSoup(page)
# fetching organisations
data3 = soup.find_all('li', class_="nav-item active")
lobbying1 = []
for element in data3:
lobbying1.append(element.span.get_text())
data.append(lobbying1)
df = pd.DataFrame(data, columns=['Organization','Groups'])
print (df)
Organization Groups
0 Department of Agric... (35) Science (35)
1 Commonwealth Scient... (8) Science (8)
2 Bureau of Meteorology (4) Science (4)
3 Tasmanian Museum an... (1) Science (1)
4 Department of Indus... (1) Science (1)
非常感谢。这完全符合我的要求。很高兴能帮上忙。顺便说一句,我非常喜欢澳大利亚;)非常感谢。这完全符合我的要求。很高兴能帮上忙。顺便说一句,我非常喜欢澳大利亚;)