Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 labelling variables of df with map() in loop
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:

when I run this code ,

Error like AttributeError:
'DataFrame' object has no attribute 'x'.
Please help also !
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.
Thanks your suggestion is correct , I found the way !

Top Page

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

Forum Jump:

Users browsing this thread: 1 Guest(s)