@@ -139,8 +139,10 @@ fragile. Type inference is a pretty big deal. So if a column can be coerced to
139
139
integer dtype without altering the contents, it will do so. Any non-numeric
140
140
columns will come through as object dtype as with the rest of pandas objects.
141
141
142
+ .. _io.fwf :
143
+
142
144
Files with Fixed Width Columns
143
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144
146
While `read_csv ` reads delimited data, the :func: `~pandas.io.parsers.read_fwf `
145
147
function works with data files that have known and fixed column widths.
146
148
The function parameters to `read_fwf ` are largely the same as `read_csv ` with
@@ -155,13 +157,11 @@ two extra parameters:
155
157
:suppress:
156
158
157
159
f = open (' bar.csv' , ' w' )
158
- data1 = """ \
159
- id8141 360.242940 149.910199 11950.7
160
- id1594 444.953632 166.985655 11788.4
161
- id1849 364.136849 183.628767 11806.2
162
- id1230 413.836124 184.375703 11916.8
163
- id1948 502.953953 173.237159 12468.3
164
- """
160
+ data1 = (" id8141 360.242940 149.910199 11950.7\n "
161
+ " id1594 444.953632 166.985655 11788.4\n "
162
+ " id1849 364.136849 183.628767 11806.2\n "
163
+ " id1230 413.836124 184.375703 11916.8\n "
164
+ " id1948 502.953953 173.237159 12468.3" )
165
165
f.write(data1)
166
166
f.close()
167
167
@@ -181,13 +181,15 @@ column specifications to the `read_fwf` function along with the file name:
181
181
df = read_fwf(' bar.csv' , colspecs = colspecs, header = None , index_col = 0 )
182
182
df
183
183
184
- Alternatively, you can supply just the column widths for contiguous columns:
184
+ Note how the parser automatically picks column names X.<column number> when
185
+ ``header=None `` argument is specified. Alternatively, you can supply just the
186
+ column widths for contiguous columns:
185
187
186
188
.. ipython :: python
187
189
188
190
# Widths are a list of integers
189
191
widths = [6 , 14 , 13 , 10 ]
190
- df = read_fwf(' bar.csv' , widths = widths, header = None , index_col = 0 )
192
+ df = read_fwf(' bar.csv' , widths = widths, header = None )
191
193
df
192
194
193
195
The parser will take care of extra white spaces around the columns
0 commit comments