Issue
I have a pandas dataframe like below
id name value1 value2 value3
=======================================================
1 AAA 1.0 1.5 1.8
2 BBB 2.0 2.3 2.5
3 CCC 3.0 3.6 3.7
I have convert the above df into something like below so that i can join it to another df based on name (which will be unique in my case)
name value
=========================
AAA [1.0, 1.5, 1.8]
BBB [2.0, 2.3, 2.5]
CCC [3.0, 3.6, 3.7]
Is there any way to achieve this instead of having too many looping statements.
Any help is much appreciated.
Thanks,
Solution
A possible solution, which applies function list
to each row of columns values
and then concatenates the two first columns of the original dataframe:
pd.concat([
df.iloc[:, :2],
df.iloc[:, 2:].apply(list, axis=1).rename('value')], axis=1)
Output:
id name value
0 1 AAA [1.0, 1.5, 1.8]
1 2 BBB [2.0, 2.3, 2.5]
2 3 CCC [3.0, 3.6, 3.7]
Answered By - PaulS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.