On the basis of list as below, I have to create a DataFrame with \"state\" and \"region\" columns:
Original data:
Alabama[edit]
Auburn (Auburn Universi
if I uderstand your question and desired output correct, you could do something like this:
univeristylist = []
with open('university_towns.txt', 'r') as file:
for line in file:
if '[edit]' in line:
state = row
else:
universitylist.append([state, row])
df = pd.DataFrame(universitylist, columns=['State', 'RegionName'])
If you don't want the '[edit]'
and '[1]'
part etc, then you could change the code to:
univeristylist = []
with open('university_towns.txt', 'r') as file:
for line in file:
if '[edit]' in line:
state = row.split(' [')[0]
else:
universitylist.append([state, row.split(' [')[0]])
df = pd.DataFrame(columns=['State', 'RegionName'])