我有以下脚本,该脚本可以满足我的需求。除此之外,Pandas数据框的每一列都有一行,如下所示。标题,摘录和网址应为列名
我该如何实现?
提前谢谢!!!!
#Get post titles & links
import pandas as pd
raw_data = pd.DataFrame([])
for container in post_containers:
# find the first <a> tag and get the text. Split the text using '/' to get an array with developer name and repo name
container_name = container.find(class_="elementor-post__title").text.split('>')[0].strip()
for link in container.find_all('a'):
link = link.get('href')
excerpt = container.find(class_="elementor-post__excerpt").text.split('>')[0].strip()
d = {'title': container_name, 'excerpt': excerpt, 'url': link}
df = pd.DataFrame(list(d.items()))
raw_data = raw_data.append(df)
答案 0 :(得分:1)
如果您的数据代表上述内容,
一个想法是将您的第一个col设置为索引然后转置。
import pandas as pd
df = pd.DataFrame({0 : ['title', 'excerpt','url'],
1 : ['title1', 'excerpt1','url1'],
2 : ['title2','excerpt2','url2']})
print(df)
0 1 2
0 title title1 title2
1 excerpt excerpt1 excerpt2
2 url url1 url2
df_new = df.set_index(0).T
print(df_new)
title excerpt url
0 title1 excerpt1 url1
1 title2 excerpt2 url2