title: Read partially unordered data into columns using pandas
body: <p>Taking part in <a href="https://adventofcode.com" rel="nofollow noreferrer">Advent of Code</a> I'm currently trying to figure out a solution for puzzle 2 of day 4. As I usually only work in Java but recently need to work in Python at work I thought it would be a good idea to do this in Python as an exercise - hence my knowledge of the language is quite low.</p>
  <p>However, the problem is the following: I'm given an input file which contains data for passports. Each passport is separated by &quot;\n\n&quot;, so there is one empty line between the data of two passports. However, a passport should contain 8 key-value pairs that can be separated either by a space character or by &quot;\n&quot; and there is no specific order to any of these fields - also it may occur that a passport doesn't contain certain fields.</p>
  <p>For better understanding I'll give you an example how two passports may look like:</p>
  <pre><code>hgt:183cm
  pid:368895060
  ecl:oth eyr:2020
  iyr:2013
  byr:1966
  
  ecl:lzr cid:279 pid:192cm
  hcl:1f7352 iyr:2014 hgt:70cm eyr:1983
  byr:2004
  </code></pre>
  <p>I thought it might be a good idea to bring that data into a table representation using pandas' data frame, however I'm not really sure how to accomplish this. My first attempt was this but it throws a ton of errors at me when executing (and I also get that it wouldn't work like this anyways):</p>
  <pre><code>import pandas as pd
  
  data = pd.read_csv('puzzle_inputs/puzzle_input_4.txt', sep=&quot;\s+&quot;, header = 0, names=[&quot;byr&quot;, &quot;iyr&quot;, &quot;eyr&quot;, &quot;hgt&quot;, &quot;hcl&quot;, &quot;ecl&quot;, &quot;pid&quot;, &quot;cid&quot;])
  data
  </code></pre>
  <p>Obviously I would need to get rid of the field identifiers like &quot;byr:&quot; but I also need them to insert the data into the right columns as for example the second pid in the example data above contains a value that could very well also be a value for height. I'm entirely confused how one would do this as I'm not used to work with data extraction in this way since I usually only build UI applications that utilize a structure database and not data files that need to be accessed dynamically.</p>
  <p>Any help is really much appreciated as I'm quite lost here.</p>