How to write lists in different directions in pandas in python -


i have 3 lists follows.

list_names_1 = ["salad", "bread"] list_names_2 = ["oil", "fat", "salt"] list_values = [[0.2, 0.1, 0.8], [0.2, 0.9, 0.8]] 

now want write aforementioned 3 lists csv file follows.

names, oil, fat, salt salad,  0.2, 0.1,  0.8 bread,  0.2, 0.9,  0.8 

that is, want list names_1 in vertical direction, list_names_2 in horizontal direction , list_values values of 2 lists.

is possible in pandas?

use dataframe constructor to_csv:

df = pd.dataframe(list_values, columns=list_names_2, index=list_names_1) df.index.name = 'names' print (df) names  oil  fat  salt salad  0.2  0.1   0.8 bread  0.2  0.9   0.8  df.to_csv('file')  names,oil,fat,salt salad,0.2,0.1,0.8 bread,0.2,0.9,0.8 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -