Home > Article > Backend Development > How do you create a Pandas DataFrame from a text file with specific patterns, where states are indicated by \"[edit]\" and regions by \"[number]\"?
Creating a Pandas DataFrame from a Text File with Specific Patterns
Problem Statement:
The goal is to create a Pandas DataFrame from a text file that has the following structure:
Alabama[edit] Auburn (Auburn University)[1] Florence (University of North Alabama) Jacksonville (Jacksonville State University)[2] Livingston (University of West Alabama)[2] Montevallo (University of Montevallo)[2] Troy (Troy University)[2] Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4] Tuskegee (Tuskegee University)[5] Alaska[edit] Fairbanks (University of Alaska Fairbanks)[2] Arizona[edit] Flagstaff (Northern Arizona University)[6] Tempe (Arizona State University) Tucson (University of Arizona) Arkansas[edit]
Where rows with "[edit]" indicate states and rows with "[number]" indicate regions. The DataFrame should split the data based on these patterns and repeat the state name for each region name.
Solution:
To achieve this, we can follow the below steps:
df = pd.read_csv('filename.txt', sep=";", names=['Region Name'])
df.insert(0, 'State', df['Region Name'].str.extract('(.*)\[edit\]', expand=False).ffill())
df['Region Name'] = df['Region Name'].str.replace(r' \(.+$', '')
df = df[~df['Region Name'].str.contains('\[edit\]')].reset_index(drop=True) print (df)
Example Output:
The output DataFrame will look as follows:
State Region Name 0 Alabama Auburn 1 Alabama Florence 2 Alabama Jacksonville 3 Alabama Livingston 4 Alabama Montevallo 5 Alabama Troy 6 Alabama Tuscaloosa 7 Alabama Tuskegee 8 Alaska Fairbanks 9 Arizona Flagstaff 10 Arizona Tempe 11 Arizona Tucson
The above is the detailed content of How do you create a Pandas DataFrame from a text file with specific patterns, where states are indicated by \"[edit]\" and regions by \"[number]\"?. For more information, please follow other related articles on the PHP Chinese website!