Space-separated issues when handling CSVs with Pandas

Asked 2 years ago, Updated 2 years ago, 49 views

I'm a beginner.
I would like to import the csv file.
The data sets are as follows (simplified).

0000   
0  12  12  12
0 123 123 123 
data=pd.read_csv('○○.csv', sep='')

If you import as above,

0 NaN NaN NaN 0 NaN NaN 0 NaN NaN 0 NaN NaN 0
0NaNaN12NaN12NaN12NaN12
0 NaN123 NaN123 NaN123 

Next, NaN is reflected in the number of spaces.
Is there any good way?

python pandas

2022-09-30 21:24

1 Answers

According to the Pandas I/O API documentation,

read_csv(filename, sep='\s+')

or

read_csv(filename,delim_whitespace=True)

It would be good if

The following options are also useful:

  • header=None—No header line.
  • skipinitialspace=True—Ignore the leading blank characters.

The following is an example of execution.For clarity, I create buffers from strings instead of reading from files.

>>import StringIO
>>import pandas as pd
>>buffer=StringIO.StringIO(""000000"   
... 0  12  12  12
... 0 123 123 123
... """)
>>>data=pd.read_csv (buffer,header=None,delim_whitespace=True)
>> data
   0    1    2    3
0  0    0    0    0
1  0   12   12   12
2  0  123  123  123

Reference URL


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.