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
x
31
31
1
NFT Collection
2
Volume (ETH)
3
Market Cap (ETH)
4
Max price (ETH)
5
Avg price (ETH)
6
Min price (ETH)
7
% Opensea+Rarible
8
#Transactions
9
#Wallets
10
Contract date
11
Axies | Axie Infinity
12
4,884
13
480,695
14
5.24
15
.0563
16
.0024
17
0
18
86,807
19
2,389,981
20
189d ago
21
Sandbox's LANDs
22
578
23
112,989
24
6
25
1.11
26
.108
27
100%
28
394
29
12,879
30
700d ago
31
Advertisement
Answer
Here’s another variation:
JavaScript
1
7
1
from io import StringIO
2
3
with open("input.txt", "r") as file:
4
data = [line.strip() for line in file]
5
data = StringIO("n".join(";".join(data[i:i+10]) for i in range(0, len(data), 10)))
6
df = pd.read_csv(data, delimiter=";")
7
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.