# Your code here
df = pd.util.testing.makeTimeDataFrame(5)
df2 = df.reset_index()
ff = lambda row: {'some key': row['A']}
# this is what I want, but my dataframe is unfortunately like df2
df.apply(ff, axis = 1)
2000-01-03 {u'some key': 1.70177486613}
2000-01-04 {u'some key': -0.833481157829}
2000-01-05 {u'some key': -0.117445006362}
2000-01-06 {u'some key': -1.12638444617}
2000-01-07 {u'some key': -0.981868953754}
Freq: B, dtype: object
# not correct
df2.apply(ff, axis = 1)
index A B C D
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
>>>
Expected Output
returns me a pandas Series with dictionary as value
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 2.7.12.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 44 Stepping 2, GenuineIntel byteorder: little LC_ALL: None LANG: en_GB
pandas: 0.18.1 nose: 1.3.7 pip: 8.1.0 setuptools: 0.6c11 Cython: 0.21 numpy: 1.10.1 scipy: 0.16.0 statsmodels: 0.6.1 xarray: None IPython: 5.1.0 sphinx: 1.3.1 patsy: 0.4.0 dateutil: 2.4.2 pytz: 2015.7 blosc: None bottleneck: None tables: 3.1.1 numexpr: 2.4.4 matplotlib: 1.4.3 openpyxl: 1.8.5 xlrd: 0.9.3 xlwt: 0.7.5 xlsxwriter: 0.5.7 lxml: 3.5.0 bs4: 4.4.1 html5lib: None httplib2: None apiclient: None sqlalchemy: 0.9.7 pymysql: None psycopg2: None jinja2: 2.8 boto: 2.32.1 pandas_datareader: None
Comment From: jreback
IIRC this was fixed n 0.18.0
are you sure the pd.show_versions()
is in the same session?
In [1]: pd.__version__
Out[1]: u'0.18.1'
In [2]: In [11]: df = pd.util.testing.makeTimeDataFrame(5)
...: ...: df2 = df.reset_index()
...: ...: ff = lambda row: {'some key': row['A']}
...: ...:
...:
In [3]: df.apply(ff, axis = 1)
...:
Out[3]:
2000-01-03 {u'some key': -0.890972464634}
2000-01-04 {u'some key': -0.888101404353}
2000-01-05 {u'some key': 1.29259890055}
2000-01-06 {u'some key': -1.54193140739}
2000-01-07 {u'some key': -1.33524721342}
Freq: B, dtype: object
Comment From: httassadar
As said in the OP, it works on a DF with time index.
Does it work for you on df2.apply where it is normal index?
Comment From: jreback
So apply tries to coerce to the original dtype. This is not very pandonic in any event. use .to_dict
or .to_json
are WAY faster. Further you could use .iterrows/.itertuples
if you really really want to do this.
In [21]: df.reset_index().astype(object).apply(ff, axis=1)
Out[21]:
0 {'some key': -0.7199673839410661}
1 {'some key': 0.277297347761761}
2 {'some key': -1.4338707486999223}
3 {'some key': 0.8916998617825874}
4 {'some key': 0.26514706847054426}
dtype: object