0

I have a list of names of countries.. and I have a large dataframe where one of the columns is ' COUNTRY ' (yes it has a space before and after the word country) I want to be create smaller DataFrames based on country names

cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[1]]

seems too long a command to achieve this? It does work though.

Now,

str("%s_data" % (asia_country_list[1]))

gives

'Taiwan_data'

but when I combine the above two:

str("%s_data" % (asia_country_list[1])) = cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[1]]

I get:

SyntaxError: can't assign to function call

happy to learn other ways as well to achieve this pls.. Thanks vm

spiff
  • 1,335
  • 3
  • 11
  • 23
  • `str("%s_data" % (asia_country_list[1]))` is a function call - you cant assign values to a function/result. You may be looking for `cleaned_df[str("%s_data" % (asia_country_list[1]))] = [...]`. Can't tell if thats the case, where do you want to store what? – Najzero Jul 18 '16 at 09:49
  • I assume you want to dynamically create variable names, in which case it might be easier to just add them to a dictionary. See [link](http://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python) – M.T Jul 18 '16 at 09:51
  • @ M.T .. indeed want to dynamically create variable names.. tx vm for taking a look, have seen the solutions below as per marmouset – spiff Jul 18 '16 at 10:21

1 Answers1

0

I don't think you should do this, but if you really need it :

exec(str("%s_data" % (asia_country_list[1])) +"= cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[1]]") 

should work. Using a dictionary is likely to solve your problem

  D={}
  D["%s_data" % (asia_country_list[1]))]=cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[1]]]

EDIT : the first solution is a bad idea : exec is a dangerous command, if one column is named "del cleaned_df" you will actually execute it, it can get destructive. Typically I am guessing spaces are a problem in your case. It's a bit like SQL injections...

Alexis Benichoux
  • 790
  • 4
  • 13
  • so that does it - both options work.. I have been reading that experts dont really like this - and you too mentioned 'don't think you shlould do this' .. why is i t? I am a newb, so want to learn the thinking behind this pls Thanks vm again for your help! – spiff Jul 18 '16 at 10:19
  • also - when I put this in a for loop: 'for country_name in len(asia_country_list): exec(str("%s_data" % (asia_country_list[country_name])) + "=cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[country_name]]")' gives me the error: TypeError: 'int' object is not iterable – spiff Jul 18 '16 at 10:29
  • v sorry for the spamming.. but the exec statement also doesnt work if the country name has a space in it: Hong Kong_data=cleaned_df[cleaned_df[' COUNTRY ']==asia_country_list[0]] SyntaxError: invalid syntax – spiff Jul 18 '16 at 10:41
  • your loop is wrong, as a start, range(len(asia_country_list) is iterable, len(asia_country_list) is not – Alexis Benichoux Jul 18 '16 at 10:41
  • thanks vm.. range(len(asia_country_list) works fine.. this is my mistake.. will read up more on data structures – spiff Jul 18 '16 at 10:43