So my dataset has some information by location for n dates. The problem is each date is actually a different column header. For example the CSV looks like
location name Jan-2010 Feb-2010 March-2010
A "test" 12 20 30
B "foo" 18 20 25
What I would like is for it to look like
location name Date Value
A "test" Jan-2010 12
A "test" Feb-2010 20
A "test" March-2010 30
B "foo" Jan-2010 18
B "foo" Feb-2010 20
B "foo" March-2010 25
problem is I don't know how many dates are in the column (though I know they will always start after name)
I guess I found a simpler solution
Concat whole
temp1
withtemp2
's columnname
You now have what you asked for.
pd.wide_to_long
You can add a prefix to your year columns and then feed directly to
pd.wide_to_long
. I won't pretend this is efficient, but it may in certain situations be more convenient thanpd.melt
, e.g. when your columns already have an appropriate prefix.You can use
pd.melt
to get most of the way there, and then sort:(Might want to throw in a
.reset_index(drop=True)
, just to keep the output clean.)Note:
pd.DataFrame.sort
has been deprecated in favour ofpd.DataFrame.sort_values
.