Home > Net >  Cast a dataframe to text following a hierarchical structure
Cast a dataframe to text following a hierarchical structure

Time:11-10

I am working in Jupyter Notebook and I have a dataframe like this

df = pd.DataFrame({'Parent': ['Stay home', "Stay home","Stay home", 'Go outside', "Go outside"], 'Child' : ['Severe weather', "raining", "Windy", 'Sunny', "Good weather"],
'Child1': ['', "some rain", "extreme windy", "very hot", ""]})


    Parent      Child           Child1
0   Stay home   Severe weather  
1   Stay home   raining         some rain
2   Stay home   Windy           exterme windy
3   Go outside  Sunny           very hot
4   Go outside  Good weather    

I want to cast this df into a txt, save it and download on my PCs' Downloads file.

However, I want to save it into a hierarchical structure:

Stay home
     severe weather
     raining
       some rain
     Windy
       exterme windy
Go outside
     Sunny
       very hot
     Good weather

I uses the pandas.DataFrame.to_string method but is not adequate for these problem.

Note: This is a subset of my dataframe which actually has many rows

Any ideas?

CodePudding user response:

Definitely not the most elegant way but I think it does the thing:

all=[]
for i in df.index:
    for j,text in enumerate(df.iloc[i]):
        if j==0:
            all.append(f'{text}\n')
        elif j==1:
            all.append(f'\t{text}\n')
        elif j==2:
            all.append(f'\t\t{text}\n')

with open('./trial.txt','w') as f:
    f.writelines(all)

Edit: I realised you don't want repetitions, then this might be closer:

df2=df.groupby(['Parent']).agg({'Child': list,'Child1': list}).reset_index()



all=[]
for i in df2.index:
    all.append(df2.iloc[i]['Parent'] '\n')
    for text1,text2 in zip(df2.iloc[i]['Child'],df2.iloc[i]['Child1']):
        all.append(f'\t{text1}\n')
        all.append(f'\t\t{text2}\n')
    
    

  • Related