0

I have a pandas dataframe that look like this:

Variable  Date    Value
A         10-12   Nan
B         10-12   20
C         10-12   Text
A         11-12   4
B         11-12   200
etc...

I want a dataframe that looks like this:

Date   A    B   C
10-12  nan  20  Text
11-12  4    200 ...
...

Now this is a rather large dataframe. I have managed to get it to work with a simple iterrows:

uniqueVariables = datadf['Variable'].unique()
uniqueDates = datadf['Date'].unique()
returndatadf = pd.DataFrame(index=uniqueDates, columns=uniqueVariables)
for index, row in datadf.iterrows():
    returndatadf.loc[row['Date'], row['Variable']] = row['Value']

This is however really slow (as I have read iterrows should be avoided when possible)

I have been trying and googling for a different solution but I have sofar not been able to find something that seems to fit my problem. I have little experience in this since speed has so far not really been of much concern to me.

Hope someone can help me.

user180146
  • 895
  • 2
  • 9
  • 18

0 Answers0