Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 labelling variables of df with map() in loop
#1
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 !
Quote
#2
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.
Quote
#3
Thanks your suggestion is correct , I found the way !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Two variables in for loop vipinv23 9 572 Aug-26-2019, 05:30 AM
Last Post: newbieAuggie2019
  Operations on indexed variables in loop Fibulavie 1 218 Aug-14-2019, 06:07 AM
Last Post: fishhook
  I need help with loop one function and creating variables don 3 540 Jan-24-2019, 07:31 AM
Last Post: buran
  I can't figure out how to create variables in a while loop MrCag 1 642 May-08-2018, 08:56 PM
Last Post: wavic
  Loop thru textfile, change 3 variables for every line herbertioz 11 3,581 Nov-10-2016, 04:56 PM
Last Post: Larz60+
  Running Class methods in a loop and updating variables. ujjwalrathod007 3 1,927 Oct-05-2016, 07:11 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)