Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Pivoting/Transposing a dataframe [duplicate]

$
0
0

I have a pandas dataframe called new_df, and it has these columns:

enter image description here

I want to I want to pivot this table so that the values in "Number" are turned into columns, while Name, Area, Buy_Date, Sell_Date, and End_date become rows which are under the column User_Information. So the new table should look like this:

enter image description here

My code is roughly like this so far:

pivoted_df = new_df.pivot_table(             index=['ID', 'Shop', 'User_Information'],             columns='Number',             values=['NAME', 'Area', 'Buy_Date', 'Sell_Date', 'End_Date']             )

But this doesn't seem to work in the way I want it, so if anyone could help me fix it I would greatly appreciate it, thank you!

My issue differs from the link given here:

How do I melt a pandas dataframe?

since I actually want to fill-in the information underneath the User_Information column. In addition, it also differs from it since the the values under Number can vary, for example, for a specific ID and Shop one of them might have only 1 column, while the one after it might have 5 etc.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>