Dexter
Dexter

Reputation: 630

Empty dataframe in pandas

hi i am new in web scraping this code works perfectly but it is printing on the last row into the csv file.I don't know why I printed values also on console and its printing all the rows

import bs4 as bs
import urllib
import pandas as pd
dfs = pd.read_html('http://www.espn.com/mlb/team/roster/_/name/nyy/new-york-yankees/' ,header= 0)

for df in dfs:
    df.to_csv('losangeles.csv', sep='\t',encoding='utf-8')

Upvotes: 2

Views: 283

Answers (2)

Igor Yudin
Igor Yudin

Reputation: 403

You shouldn't iterate DataFrame. You actually get each row and save it to the same file.

To save all DataFrame in one file, do so:

dfs.to_csv('losangeles.csv', sep='\t',encoding='utf-8')

Upvotes: 1

jezrael
jezrael

Reputation: 863301

There is problem you overwrite file, always write df to same filename.

Solution is create unique file names, e.g. add count by enumerate:

for i, df in enumerate(dfs):
    df.to_csv('losangeles_{}.csv'.format(i), sep='\t',encoding='utf-8')

Upvotes: 0

Related Questions