Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate a dataframe from a string

Inspired by this solution I have been using the following code to clean-up some data that I obtain using Beautiful Soup:

nfl = soup.findAll('li', "player")
lines = ("{}. {}\n".format(ind,span.get_text(strip=True).rstrip("+"))
         for ind, span in enumerate(nfl,1))
print("".join(lines))

The problem is that the output of this comes in the format of a string and I would like to store each one of it’s lines as a different row in a dataframe. I tried introducing the code in a loop but that would not do. The best I could manage was to store the same string n times into my desired dataframe. Could you help me out?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:

nfl = soup.findAll("li", "player")

all_data = []
for span in nfl:
    all_data.append({"player": span.get_text(strip=True).rstrip("+")})

df = pd.DataFrame(all_data)
print(df)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading