I am reading a text file using pd.read_fwf
as below:
import pandas as pd
specs_test =[(19, 20),(20, 21),(21, 23),(23,26)]
names_test = ["Record_Type","Resident_Status","State_Occurrence_FIPS",
"County_Occurrence_FIPS"]
test_l = pd.read_fwf('test.txt', header=None, names = names_test, colspecs= specs_test)
and test.txt is as follow:
11SC059
11SC051
11SC019
11SC033
11SC007
11SC041
22SC079
11SC043
11SC045
22SC079
after reading the file test_l is as follow:
Record_Type Resident_Status State_Occurrence_FIPS County_Occurrence_FIPS
0 1 S C0 59
1 1 S C0 51
2 1 S C0 19
3 1 S C0 33
4 1 S C0 7
5 1 S C0 41
6 2 S C0 79
7 1 S C0 43
8 1 S C0 45
9 2 S C0 79
but, based on my colspec it should have the following (I have just added first row as I expected):
1 1 SC 059
What am I missing here? Thank you very much for your help!
First you are off by an index. Try:
Also, for numerical values the leading zeros will be ignored. To keep them, you can convert to string by adding:
The final code can be:
The result: