I have the following dataframe, but i want the Attributes to be columns and the corresponding data to transpose..any ideas please as am quite stuck on this?
Asked
Active
Viewed 35 times
1
-
Please include any relevant information [as text directly into your question](https://stackoverflow.com/editing-help), do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) – Henry Ecker May 24 '21 at 17:33
-
Please include a _small_ subset of your data as a __copyable__ piece of code that can be used for testing as well as your expected output for the __provided__ data. See [MRE - Minimal, Reproducible, Example](https://stackoverflow.com/help/minimal-reproducible-example), and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker May 24 '21 at 17:34
1 Answers
1
I'm supposing you have this DataFrame:
Attribute Value
0 DESCRIPTION XXX
1 Price HLDS 114.8
2 DESCRIPTION YYY
3 Price HLDS 11.0255
Then:
print(
df.pivot(columns="Attribute", values="Value")
.apply(lambda x: sorted(x, key=lambda k: pd.isna(k)))
.dropna()
.rename_axis("", axis=1)
)
Prints:
DESCRIPTION Price HLDS
0 XXX 114.8
1 YYY 11.0255

Andrej Kesely
- 168,389
- 15
- 48
- 91