Python数据帧数据是通过索引而不是行来显示的

2024-09-30 01:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学习Python。我在为书评练习网络阅读。抓取工作正常,我正在“附加”每个页面的数据,然后将其全部转储到数据框中。我的问题是,我不知道如何让数据以列、行而不是列、索引的形式显示。我想把所有的书标题放在“titles”下,把作者放在“authors”下,等等。每个索引都是一个不同的网页。此示例包括三个刮取的URL。我的数据帧代码是:

titlesFin = []
authorsFin = []
ratingsFin = []
reviewsFin = []

# find container with all books per page
    books = soup.find_all('tr', itemtype="http://schema.org/Book")

    titles = [removeWhite(title.find('a', class_='bookTitle').text).strip()
              for title in books]
    titlesFin.append(titles)
    authors = [author.find('a', class_='authorName').text for author in books]
    authorsFin.append(authors)
    reviews = soup.find_all('span', class_='minirating')
    reviewNums = [find_numbers(rate.text.strip()) for rate in reviews]
    ratings1, numRev = zip(*reviewNums)
    ratingsFin.append(ratings1)
    reviewsFin.append(numRev)

bookList = pd.DataFrame({
    'Title': titlesFin,
    'Author': authorsFin,
    'Rating': ratingsFin,
    'Reviews': reviewsFin, })

bookList.to_csv('GoodReadsxx.csv')
print(bookList)

我当前的数据帧输出:

                                               Title  \
0  [The Hunger Games (The Hunger Games, #1), Harr...   
1  [Moby-Dick or, the Whale, The Red Tent, The Se...   
2  [Thirteen Reasons Why, Wicked: The Life and Ti...   

                                              Author  \
0  [Suzanne Collins, J.K. Rowling, Harper Lee, Ja...   
1  [Herman Melville, Anita Diamant, Sue Monk Kidd...   
2  [Jay Asher, Gregory Maguire, James Clavell, Jo...   

                                              Rating  \
0  (4.33, 4.50, 4.28, 4.26, 3.59, 4.37, 3.94, 4.2...   
1  (3.50, 4.18, 4.05, 4.33, 4.56, 4.01, 4.26, 4.5...   
2  (3.93, 3.53, 4.39, 3.98, 4.06, 4.20, 4.35, 4.2...   

                                             Reviews  
0  (6,144,447, 2,387,048, 4,310,467, 2,855,826, 4...  
1  (466,455, 506,936, 1,089,483, 631,246, 2,462,7...  
2  (744,214, 563,831, 141,685, 371,866, 448,159, ... 

来自两个列表的数据:

titlesFin = [['The Hunger Games (The Hunger Games, #1)',
  'Harry Potter and the Order of the Phoenix (Harry Potter, #5)',
  'To Kill a Mockingbird',
  'Pride and Prejudice',
  'Twilight (Twilight, #1)',
  'The Book Thief',
  'Animal Farm',
  'The Chronicles of Narnia (Chronicles of Narnia, #1-7)',
  'J.R.R. Tolkien 4-Book Boxed Set: The Hobbit and The Lord of the Rings',
  'Gone with the Wind',

authorsFin:[['Suzanne Collins',
  'J.K. Rowling',
  'Harper Lee',
  'Jane Austen',
  'Stephenie Meyer',
  'Markus Zusak',
  'George Orwell',
  'C.S. Lewis',
  'J.R.R. Tolkien',
  'Margaret Mitchell',
  'John Green',
  'Douglas Adams',
  'Shel Silverstein',
  'Emily Brontë',
  'Dan Brown',

Tags: andofthe数据findbooksauthorsgames

热门问题