Reputation: 667
I am trying to parse a txt.file that has a known structure. Each column represent a country and each value the frequency in which the name is used in this country.
More info about the source plus original doc here - it is called nam_dict.txt
It look like this:
M Aadam 1 $
F Aadje 1 $
M Ådne + 1 $
M Aadu 12 $
?F Aaf 1 $
F Aafke 4 $
? Aafke 1 $
F Aafkea 1 $
M Aafko 1 $
M Aage 761 $
M Åge + 56 $
F Aagje 1 2 $
Problem is that there is no separator between digits and any figure for a given column could have one or two digits (1 to 13). Is there a good way to extract this?
I would like to do this with Python and Pandas.
Columns are as follows:
['GreatBritain',
'Ireland',
'U.S.A.',
'Italy',
'Malta',
'Portugal',
'Spain',
'France',
'Belgium',
'Luxembourg',
'theNetherlands',
'EastFrisia',
'Germany',
'Austria',
'Swiss',
'Iceland',
'Denmark',
'Norway',
'Sweden',
'Finland',
'Estonia',
'Latvia',
'Lithuania',
'Poland',
'CzechRepublic',
'Slovakia',
'Hungary',
'Romania',
'Bulgaria',
'BosniaandHerzegovina',
'Croatia',
'Kosovo',
'Macedonia',
'Montenegro',
'Serbia',
'Slovenia',
'Albania',
'Greece',
'Russia',
'Belarus',
'Moldova',
'Ukraine',
'Armenia',
'Azerbaijan',
'Georgia',
'Kazakhstan/Uzbekistan,etc.',
'Turkey',
'Arabia/Persia',
'Israel',
'China',
'India/SriLanka',
'Japan',
'Korea',
'Vietnam',
'othercountries']
Hoping to get to this type of output (with numbers where appropriate):
gender GreatBritain Ireland U.S.A. Italy Malta Portugal Spain France Belgium ... Kazakhstan/Uzbekistan,etc. Turkey Arabia/Persia Israel China India/SriLanka Japan Korea Vietnam othercountries
name
Aad M NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Aadam M NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Aadje F NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Ådne M NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Aadu M NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Upvotes: 2
Views: 408
Reputation: 2405
Whitespaces can be combined:
df = pd.read_csv(filename, sep="\s+")
E.g. several consequent whitespaces will be treated as single delimiter.
Update: Seems like you ought to use read_fwf
>>> df = pd.read_fwf('nam_dict.txt', header=None, skiprows=362)
>>> df.head()
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 M Aad NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN NaN $
1 M Aadam NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN $
2 F Aadje NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN NaN $
3 M Ådne + NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN $
4 M Aadu NaN NaN NaN NaN 12 NaN NaN NaN NaN NaN NaN $
>>> df.shape
(48528, 14)
Update2: Had to generate list of columns. Width of 1st column=1, second = 27 (until the + sign), and remaining ones set to 1. Check and amend, if needed:
>>> cols = [2, 27]
>>> cols.extend([1]*58)
>>> df = pd.read_fwf('nam_dict.txt', header=None, skiprows=362, widths=cols)
>>> df.head()
0 1 2 3 4 5 6 7 8 9 ... 50 51 52 53 54 \
0 M Aad NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
1 M Aadam NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
2 F Aadje NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
3 M Ådne + NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
4 M Aadu NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN
55 56 57 58 59
0 NaN NaN NaN NaN $
1 NaN NaN NaN NaN $
2 NaN NaN NaN NaN $
3 NaN NaN NaN NaN $
4 NaN NaN NaN NaN $
[5 rows x 60 columns]
Upvotes: 2