Question:
I got a DataFrame looks like this (call it df1):
I need to convert the value to a matrix for calculation, so I think I need to transform the DataFrame to this form (call it df2) first (and then convert to a numpy array):
if an id don’t have value on a day, just set that day’s value to 0
(and probably none of the ids
have a full-date value).
What I think is to generate an empty DataFrame (call it df3) first and then fill df1’s data in it:
But I don’t know the proper way to iterate df1’s value to match the cell in df3 (And people say it’s a bad idea to iterate a dataframe ?), or is there a better approach (like pivot or merge)?
Answer:
This should work.
If you have better answer, please add a comment about this, thank you!