I'm having a little trouble with this maybe someone could direct me in the right direction here.
Suppose I have a data frame that looks as follows (actual dataset has many more entries and idents):
open ident
2011-01-01 00:00:00 -1.252090 df1
2011-01-01 01:00:00 -1.427444 df1
2011-01-01 02:00:00 -0.415251 df1
2011-01-01 03:00:00 -0.797411 df1
2011-01-01 04:00:00 -0.515046 df1
2011-01-01 00:00:00 1.107162 df2
2011-01-01 01:00:00 0.073243 df2
2011-01-01 02:00:00 0.224991 df2
2011-01-01 03:00:00 -1.269277 df2
2011-01-01 04:00:00 0.468960 df2
Is there any quick way to reformat the data frame to look as such?
df1 df2
2011-01-01 00:00:00 -1.252090 1.107162
2011-01-01 01:00:00 -1.427444 0.073243
2011-01-01 02:00:00 -0.415251 0.224991
2011-01-01 03:00:00 -0.797411 -1.269277
2011-01-01 04:00:00 -0.515046 0.468960
I've played around with groupby and transpose to no avail, any tips would be great appreciated.
Pandas Series: transform() functionThe transform() function is used to call function on self producing a Series with transformed values and that has the same axis length as self. Function to use for transforming the data. If a function, must either work when passed a Series or when passed to Series. apply.
You can use the following basic syntax to convert a pandas DataFrame from a wide format to a long format: df = pd. melt(df, id_vars='col1', value_vars=['col2', 'col3', ...])
transform() can take a function, a string function, a list of functions, and a dict. However, apply() is only allowed a function. apply() works with multiple Series at a time. But, transform() is only allowed to work with a single Series at a time.
You can use the pivot
function:
df.pivot(index='date', columns='variable', values='value')
For more info see: http://pandas.pydata.org/pandas-docs/stable/reshaping.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With