Skip to content Skip to sidebar Skip to footer

How To Print Specific Output Based On Condition From Two Variables

I have a dataframe like that : And I would like an output like that : name1 : mission1 : sentences1 sentences2 mission2 : sentences3 name2 : mission1 : sentences4 name3 : mission1

Solution 1:

You can optimize the code for your own dataframe but the logic is the same:

My DataFrame:

enter image description here

Code

for name in df.name.unique():
    print(name)
    for miss in df.mission.unique():
        print('  ' + miss)
        for sentence in df.sentences[(df.name == name) & (df.mission == miss)].tolist():
            print('    ' + sentence)

Output

name1
  mission1
    sentence1
    sentence2
  mission2
    sentence3
name2
  mission1
    sentence4
  mission2
name3
  mission1
    sentence5
  mission2
    sentence6

Post a Comment for "How To Print Specific Output Based On Condition From Two Variables"