Пример очистки этого набора данных можно найти в учебном пособии Чистка данных Pythonic с помощью NumPy и Pandas .
Вариант 1: выполнять обработку строк в "Pure Python"
Вы можете использовать жадный цикл for над строками файла и загрузить его за O (n) раз:
import pandas as pd
university_towns = []
with open('input/university_towns.txt') as file:
for line in file:
edit_pos = line.find('[edit]')
if edit_pos != -1:
# Remember this `state` until the next is found
state = line[:edit_pos]
else:
# Otherwise, we have a city; keep `state` as last-seen
parens = line.find(' (')
town = line[:parens] if parens != -1 else line
university_towns.append((state, town))
towns_df = pd.DataFrame(university_towns,
columns=['State', 'RegionName'])
Вариант 2: выполнять обработку строк через Pandas API
В качестве альтернативы, вы можете выполнять обработку строк с помощью средства доступа .str
Pandas:
import re
import pandas as pd
university_towns = []
with open('input/university_towns.txt') as file:
for line in file:
if '[edit]' in line:
# Remember this `state` until the next is found
state = line
else:
# Otherwise, we have a city; keep `state` as last-seen
university_towns.append((state, line))
towns_df = pd.DataFrame(university_towns,
columns=['State', 'RegionName'])
towns_df['State'] = towns_df.State.str.replace(r'\[edit\]\n', '')
towns_df['RegionName'] = towns_df.RegionName\
.str.strip()\
.str.replace(r' \(.*', '')\
.str.replace(r'\[.*', '')
Выход:
>>> towns_df.head()
State RegionName
0 Alabama Auburn
1 Alabama Florence
2 Alabama Jacksonville
3 Alabama Livingston
4 Alabama Montevallo