Create Pandas DataFrame from txt file with specific pattern

前端 未结 6 2440
北海茫月
北海茫月 2020-11-22 09:04

I need to create a Pandas DataFrame based on a text file based on the following structure:

Alabama[edit]
Auburn (Aubu         


        
6条回答
  •  情深已故
    2020-11-22 09:24

    You seem to be from Coursera's Introduction to Data Science course. Passed my test with this solution. I would advice not copying the whole solution but using it just for refrence purpose :)

    lines = open('university_towns.txt').readlines()
    
    l=[]
    lofl=[]
    flag=False
    for line in lines:
        l = []
        if('[edit]' in line):
            index = line[:-7]
        elif('(' in line):
            pos = line.find('(')
            line = line[:pos-1]
            l.append(index)
            l.append(line)
            flag=True
        else:
            line = line[:-1]
            l.append(index)
            l.append(line)
            flag=True
        if(flag and np.array(l).size!=0):
            lofl.append(l)
    df = pd.DataFrame(lofl,columns=["State","RegionName"])
    

提交回复
热议问题