I have the following dictionary:

```
details = {"Primary_key" : [{'key_1': 'val',  'key_3': "val", 'key_5': ['val_1', 'val_2', 'val_3'], 'key_6': 'val'}, {'key_2': 'val', 'key_3': 'val', 'key_5': ['val_1','val_2'], 'key_6': 'val'}, {'key_1': 'val', 'key_2': 'val', 'key_3': 'val', 'key_4': 'val', 'key_5': ['val_1', 'val_2'], 'key_6': 'val'}] }

```

I have the following code which converts this into a csv file.

```
    import pandas as pd
    for name,val in details.items():
        df = pd.DataFrame.from_dict(details[name])
        df.index = [name]*len(df)
        print df.index
        with open("my_file.csv",'a') as f:
            df.to_csv(f)

```

The key\_x being the header, primary\_key being the name and val as the text, I got the following output(example of the output).[![Output of the code](https://i.stack.imgur.com/FemP9.jpg)](https://i.stack.imgur.com/FemP9.jpg).

Is there a way I can get the csv file in the following format?[![desired format](https://i.stack.imgur.com/shRPf.jpg)](https://i.stack.imgur.com/shRPf.jpg)