I'm quite new to Python and I'm trying to use Pandas (in iPython Notebook, Python 3) to combine three columns. This is the original data:
RegistrationID FirstName MiddleInitial LastName
1 John P Smith
2 Bill Missing Jones
3 Paul H Henry
And I'd like to have:
RegistrationID FirstName MiddleInitial LastName FullName 1 John P Smith Smith, John, P 2 Bill Missing Jones Jones, Bill 3 Paul H Henry Henry, Paul, H
I'm sure this is absolutely not the correct way of doing this, but this is how I have set it up so far in a for loop. Unfortunately, it just keeps going and going and never finishes.
%matplotlib inline
import pandas as pd
from IPython.core.display import HTML
css = open('style-table.css').read() + open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))
reg = pd.DataFrame.from_csv('regcontact.csv', index_col=RegistrationID)
for item, frame in regcombo['MiddleInitial'].iteritems():
while frame == 'Missing':
reg['FullName'] = reg.LastName.map(str) + ", " + reg.FirstName
else: break
The idea is then to add another column for those with complete names (i.e. including MiddleInitial):
for item, frame in regcombo['MiddleInitial'].iteritems():
while frame != 'Missing':
reg['FullName1'] = reg.LastName.map(str) + ", " + reg.FirstName + ", " + reg.MiddleInitial
else: break
And then combine them, so that there are no null values. I've looked everywhere, but I can't quite figure it out. Any help would be appreciated, and I apologize in advance if I have broken any conventions, as this is my first post.