-3

I'm trying to create a bunch of new Binary variables just for some columns that contain a certain word (and I want to name these new binary variables BINARY_ +column name), I'm trying to do it in this was way but it doesn't work:

# create empty list
List_of_dummy_names = [] 

# word
string = "WORD"

for col in list(df.columns.values):
    if string in df.columns.values[col]:
        List_of_dummy_names.append('BINARY_'+col)
Goralight
  • 2,067
  • 6
  • 25
  • 40
Mastodon87
  • 325
  • 3
  • 6
  • 14

2 Answers2

0

In your case, col looks like some kind of a collection. You probably want to do this:

List_of_dummy_names.append('BINARY_'+string)
jurez
  • 4,436
  • 2
  • 12
  • 20
0

if you want to rename pandas dataframe column with the new created List_of_dummy_names (elements with 'BINARY_'+column_name) then you can follow my answer.

lets say

cv = list(df.columns.values)
#cv = ['aword', 'bword', 'c']
search_String = 'word'
replace_dict = dict(zip(cv,['BINARY_'+x if search_String in x else x for x in cv]))
#{'aword': 'BINARY_aword', 'bword': 'BINARY_bword', 'c': 'c'}
#Then in pandas dataframe rename method, use this dictinary
new_df = df.rename(col=replace_dict)

Also check if you could use the below

List_of_dummy_names = ['BINARY_'+x for x in cv if search_String in x ]
#['BINARY_aword', 'BINARY_bword']  #filters the element having 'word' in them and prefixed with 'BINARY_'

Check if you need this (Cause i am confused about 'what you are looking for')

#df has only one column named 'col_to_replace'
col_to_replace
   aword
   bword
   c
df['col_to_replace'] = ['BINARY_'+x if search_String in x else x for x in df['col_to_replace']]
#col_to_replace
   BINARY_aword  #prefixed
   BINARY_bword  #prefixed
   c             #word not found, so as it was

Now you got your new list of column names in a list.

List_of_dummy_names #['BINARY_aword', 'BINARY_bword']
#loop over it and create new columns in existing dataframe
for col_Name in List_of_dummy_names:
    df[col_Name] = 'default_value_1' #it will create new column "BINARY_aword" and all the row_values as string 'default_value_1' for first loop and in 2nd loop new column "BINARY_aword" with all values as 'default_value_1'.

if you already have a values in a list with len(list) == len(df) then assign that list as df[col_Name] = list_of_values_having_same_length_as_DF

Satya
  • 5,470
  • 17
  • 47
  • 72
  • Hi! thanks for your suggestion, but I'm trying to add these new binary variables to the dataframe – Mastodon87 Oct 19 '17 at 09:36
  • @JamesPietroZanzarelli- then use List_of_dummy_names = ['BINARY_'+x for x in cv if search_String in x] then use this list as values, else use the above dictionary and call a "map" function over a dataframe column to replace values. – Satya Oct 19 '17 at 09:41
  • This is really slow. If you're working with pandas, these aren't the types of commands you should be giving. – cs95 Oct 19 '17 at 10:01
  • yup, I know. but tried to help, i have also suggested OP to use a map. – Satya Oct 19 '17 at 10:03
  • now I have this List_of_dummy_names, is it possible to attached these as additional columns to an existing data frame? If so, how can I do it? – Mastodon87 Oct 19 '17 at 11:26
  • Great. in that case, under columns what values do you want to put in??if any default value, then check the edit. – Satya Oct 19 '17 at 12:33