labelling variables of df with map() in loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: labelling variables of df with map() in loop (/thread-16780.html) |
labelling variables of df with map() in loop - theinzawoo - Mar-14-2019 These are in panda df , agree={'1':'agree','0':"don't agree"} list_agg=["anc_ir_fo_daily","anc_att_vac_neg", "anc_iodine","anc_danger_neg", "bf_firsthour", "bf_sixmonths_neg", "bf_prev_dehy_neg", "gen_ors_zinc" ] I want to label list_agg with agree dictionary in looping My code : for x in list_agg: df.x=df.x.map(agree) when I run this code , Error like AttributeError: 'DataFrame' object has no attribute 'x'. Please help also ! RE: labelling variables of df with map() in loop - scidam - Mar-14-2019 I am not sure if this could help you to solve the problem you are trying to do, but it is better to use .loc[:, x] pandas indexier instead of direct accessing df.x . Moreover, if you assign column name to a variable, using .loc[:, varname] is (almost) the only right way to get data in the column.In general, direct access to data in a column, e.g. by df.column_name could cause even unpredictable behavior if you occasionally touch some DataFrame method instead, e.g. If you have a column named ix , you can not get access to it by df.ix because.ix is Pandas indexier with predefined behavior.
RE: labelling variables of df with map() in loop - theinzawoo - Mar-19-2019 Thanks your suggestion is correct , I found the way ! |