Skip to content
Advertisement

Convert Text File into Pandas Dataframe

I want to create a dataframe from a textfile. I scraped some data from a website and wrote it into a .txt file. There are 10 ‘columns’, as shown in the first 10 lines of the text file. Can anyone help me with seperating the lines into the respective columns in a pandas dataframe format? Much appreciated!

The following is an example of the text file. I would like the first 10 lines to be the column names and the subsequent lines to be under the respective columns.

JavaScript

Advertisement

Answer

Here’s another variation:

JavaScript

The upside: You don’t have to convert numbers from string to int/float etc., the pd.read_csv does it. The downside: You have to make sure the delimiter (used in the join and pd.read_csv) is a character that doesn’t occur in the input.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement