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.

NFT Collection
Volume (ETH)
Market Cap (ETH)
Max price (ETH)
Avg price (ETH)
Min price (ETH)
% Opensea+Rarible
#Transactions
#Wallets
Contract date
Axies | Axie Infinity
4,884
480,695
5.24
.0563
.0024
0
86,807
2,389,981
189d ago
Sandbox's LANDs
578
112,989
6
1.11
.108
100%
394
12,879
700d ago

Advertisement

Answer

Here’s another variation:

from io import StringIO

with open("input.txt", "r") as file:
    data = [line.strip() for line in file]
data = StringIO("n".join(";".join(data[i:i+10]) for i in range(0, len(data), 10)))
df = pd.read_csv(data, delimiter=";")

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