Reputation: 771
Hello I am trying to transpose a table in dataframe as follow, where A and B are both companies name.
This is the dataframe I have so far
|---------------------|------------------|------------------|
| Date | A | B |
|---------------------|------------------|------------------|
| date_1 | 34 | 8 |
|---------------------|------------------|------------------|
| date_2 | | 12 |
|---------------------|------------------|------------------|
| date_3 | 6 | 321 |
|---------------------|------------------|------------------|
and this is what I am looking to achieve:
|---------------------|------------------|------------------|
| Date | Company | Value |
|---------------------|------------------|------------------|
| date_1 | A | 34 |
|---------------------|------------------|------------------|
| date_1 | B | 8 |
|---------------------|------------------|------------------|
| date_2 | B | 12 |
|---------------------|------------------|------------------|
| date_3 | A | 6 |
|---------------------|------------------|------------------|
| date_3 | B | 321 |
|---------------------|------------------|------------------|
Upvotes: 1
Views: 43
Reputation: 16182
You are probably looking for melt, which should give you what you want.
pd.melt(df, id_vars=['Date'],value_vars=['A','B'], var_name='Company',value_name='Value').dropna()
Upvotes: 3