-2

I am trying to use pandas read an append list and show each column, but it read each list as a column

li= []
for k in sample:
    lines = open(k).read().splitlines()
    for i in range(num):
       lines = random.choice(lines)
       lines = lines.replace('\t',' ')
       li.append(lines)   
       data = DataFrame(li)

'''

where the sample is a serious of files, and the result looks like:

                0
0 1.0 1.0 1.0 ...
1 1.0 1.0 1.0 ...
.
.
.

but I would like it as:

    0    1   2 ...
0  1.0 1.0 1.0 ...
1  1.0 ...
.
.
.
Aisling
  • 1
  • 3
  • split the lines on separators. Look at any similar question that works off of csvs, yours is a csv with just a different separator (tab or space delimited). – Paritosh Singh Apr 07 '19 at 11:12
  • Possible duplicate of [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Paritosh Singh Apr 07 '19 at 11:16

1 Answers1

0

It appears you have either TSV or a space delimited files. Pandas can read that directly be choosing the appropriate sep argument.

If those are tabs between the cells it would look like: pandas.read_csv(‘your/file/path.txt’, sep=‘\t’)

For spaces set sep to sep=‘ ‘

To read multiple files, you would do this and then append or concat the Dataframes together.

Erik Z
  • 412
  • 3
  • 11