Issue
I have a dataframe with a column containing a list with values to replace each column, and I'm not sure how to move the list to do this. Here is an example dataframe:
A B C D
2020-07-31 0 0 0 [2,3,4]
2020-08-31 0 0 0 [5,6,7]
2020-09-30 0 0 0 [8,9,10]
2020-10-31 0 0 0 [0,1,2]
I would want to replace column A, B, and C with D like so:
A B C
2020-07-31 2 3 4
2020-08-31 5 6 7
2020-09-30 8 9 10
2020-10-31 0 1 2
What is the most efficient way to do this?
EDIT: column D is in the original dataframe, I want to move the values from D into A, B, and C and then drop the column D
Solution
You can use:
df[:] = df.pop('D').to_list()
# or specifying columns
# df[['A', 'B', 'C']] = df.pop('D').tolist()
print(df)
# Output
A B C
2020-07-31 2 3 4
2020-08-31 5 6 7
2020-09-30 8 9 10
2020-10-31 0 1 2
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.