Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas read data without header or index

Here is the .csv file :

0   0   1   1   1   0   1   1   0   1   1   1   1
0   1   1   0   1   0   1   1   0   1   0   0   1
0   0   1   1   0   0   1   1   1   0   1   1   1
0   1   1   1   1   1   1   1   1   1   1   1   2
0   1   1   1   0   1   1   1   1   1   1   1   1
0   0   0   1   1   1   0   1   0   0   0   1   1
0   0   0   0   1   1   0   0   1   0   1   0   2
0   1   1   0   1   1   1   1   0   1   1   1   1
0   0   1   0   0   0   0   0   0   1   1   0   1
0   1   1   1   0   1   1   0   0   0   0   1   1

where the first column must be indices like (0,1,2,3,4 ...) but due to some reasons they are zeros. Is there any way to make them normal when reading the csv file with pandas.read_csv ?

i use

df = pd.read_csv(file,delimiter='\t',header=None,names=[1,2,3,4,5,6,7,8,9,10,11,12]) 

and getting something like:

    1   2   3   4   5   6   7   8   9   10  11  12
0   0   1   1   1   0   1   1   0   1   1   1   1
0   1   1   0   1   0   1   1   0   1   0   0   1
0   0   1   1   0   0   1   1   1   0   1   1   1
0   1   1   1   1   1   1   1   1   1   1   1   2
0   1   1   1   0   1   1   1   1   1   1   1   1
0   0   0   1   1   1   0   1   0   0   0   1   1
0   0   0   0   1   1   0   0   1   0   1   0   2
0   1   1   0   1   1   1   1   0   1   1   1   1
0   0   1   0   0   0   0   0   0   1   1   0   1
0   1   1   1   0   1   1   0   0   0   0   1   1

and it's nearly i need, but first column (indices) is still zeros. Can pandas for example ignore this first column of zeros and automatically generate new indices to get this:

  0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 1 0 1 1 0 0 0 1 1  1  0  1
1 0 1 0 1 1 0 0 0 1 1  1  1  2
2 0 1 1 1 0 0 1 1 1 1  1  1  2
like image 444
Василий Масов Avatar asked May 02 '18 19:05

Василий Масов


People also ask

How can I read pandas without header?

Use read_csv method to get the DataFrame with tab separator and without headers. To read without headers, use header=0.

How do I read a Dataframe without an index in Python?

In order to export pandas DataFrame to CSV without index (no row indices) use param index=False and to ignore/remove header use header=False param on to_csv() method.

How do I skip the header in pandas?

You can set the header option to None to ignore header.

How do I read a CSV file without the header in Python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.


2 Answers

You might want index_col=False

df = pd.read_csv(file,delimiter='\t', 
                 header=None, 
                 index_col=False) 

From the Docs,

If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index

like image 182
rafaelc Avatar answered Oct 20 '22 01:10

rafaelc


Why fuss over read_csv? Use np.loadtxt:

pd.DataFrame(np.loadtxt(file, dtype=int))

   0   1   2   3   4   5   6   7   8   9   10  11  12
0   0   0   1   1   1   0   1   1   0   1   1   1   1
1   0   1   1   0   1   0   1   1   0   1   0   0   1
2   0   0   1   1   0   0   1   1   1   0   1   1   1
3   0   1   1   1   1   1   1   1   1   1   1   1   2
4   0   1   1   1   0   1   1   1   1   1   1   1   1
5   0   0   0   1   1   1   0   1   0   0   0   1   1
6   0   0   0   0   1   1   0   0   1   0   1   0   2
7   0   1   1   0   1   1   1   1   0   1   1   1   1
8   0   0   1   0   0   0   0   0   0   1   1   0   1
9   0   1   1   1   0   1   1   0   0   0   0   1   1

The default delimiter is whitespace, and no headers/indexes are read in by default. Column types are also not inferred, since the dtype is specified to be int. All in all, this is a very succinct and powerful alternative.

like image 35
cs95 Avatar answered Oct 20 '22 02:10

cs95