Dec-03-2018, 04:57 PM
So I'm new to python and obviusley new to "pythonic" code,
the problem I am facing is coverting a DataFrame value to a tuple.
For example a feature called MSZoning have the following options:
A, C, FV, I, RH, RL, RP, RM
I want to convert any value of that feature to a tuple,
for example if the value for MSZoning in a row is "RH" I want to convert it to: (0, 0, 0, 0, 1, 0, 0, 0).
The code I have right now is this:
"write a function !" my java-oriented brain said,
"it will take a DF, a feature name and a new name as input and will return the DS after converting the column's values !"
This is the "half-pythonic" function I wrote:
is writing small functions by yourself something a python developer will do?
Thank you so much for taking the time to help.
the problem I am facing is coverting a DataFrame value to a tuple.
For example a feature called MSZoning have the following options:
A, C, FV, I, RH, RL, RP, RM
I want to convert any value of that feature to a tuple,
for example if the value for MSZoning in a row is "RH" I want to convert it to: (0, 0, 0, 0, 1, 0, 0, 0).
The code I have right now is this:
# coverting string values to tuples train_df["Zone"] = train_df['MSZoning'].map({'A': (1, 0, 0, 0, 0, 0, 0, 0), 'C': (0, 1, 0, 0, 0, 0, 0, 0), 'FV': (0, 0, 1, 0, 0, 0, 0, 0), 'I': (0, 0, 0, 1, 0, 0, 0, 0), 'RH': (0, 0, 0, 0, 1, 0, 0, 0), 'RL': (0, 0, 0, 0, 0, 1, 0, 0), 'RP': (0, 0, 0, 0, 0, 0, 1, 0), 'RM': (0, 0, 0, 0, 0, 0, 0, 1) }) # dropping unnesesery column train_df.drop(['MSZoning'])now, I want to put trough the same procedure more than 10 features - which seems tidious and not so much pythonic.
"write a function !" my java-oriented brain said,
"it will take a DF, a feature name and a new name as input and will return the DS after converting the column's values !"
This is the "half-pythonic" function I wrote:
def feature_to_boolean_tuple(df, feature_name, new_name): tuple_list = [] #each tuple will represent an option feature_options = df[feature_name].unique() feature_options_length = len(feature_options) # creating a list the size of feature_options_length, all 0's list_to_be_tuple = [0 for i in range(feature_options_length)] for i in range(feature_options_length): list_to_be_tuple[i] = 1 # inserting 1 representing option number i tuple_list.append(tuple(list_to_be_tuple)) list_to_be_tuple[i] = 0 mapping = dict(zip(feature_options, tuple_list)) # dict from values to vectors df[new_name] = df[feature_name].map(mapping) df.drop([feature_name], axis=1, inplace=True)but is it really the pythonic way of solving this problem?
is writing small functions by yourself something a python developer will do?
Thank you so much for taking the time to help.