I am trying to print a df to csv to later export to a Tableau visualization. I can do what I want in Pandas using the labels of my columns, however, to easily work in Tableau, I need to present my data in certain format.
This is my pandas df:
company size year Australia Canada Caribbean
1 USAA 315.0 1997 200.0 115.0 0
2 PIMCO 165.0 1997 165.0 0.0 0
3 Swiss Re 18.0 1997 5.0 10.0 3.0
I would like to re-shape it so it looks like this more or less:
company size year value value
1 USAA 315.0 1997 Australia 200.0
2 USAA 315.0 1997 Canada 115.0
3 PIMCO 165.0 1997 Australia 165.0
4 Swiss Re 18.0 1997 Australia 5.0
5 Swiss Re 18.0 1997 Canada 10.0
6 Swiss Re 18.0 1997 Caribbean 3.0
Is there a way to do this easily in Pandas that does not involve creating multiple groupbys?
I had a similar problem, and after trying multiple approaches, this is the most pythonic way I found:
df = pd.melt(df, id_vars=['company', 'size', 'year'])
df = df[df.value != 0]
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.