python - Is there a multiple column map function for dataframes? -


In pandes, how can one column be obtained from many other columns? For example, I could say that I wanted to annotate my dataset with the address of the correct address for each topic. Maybe to label some plots - so I can tell the result for whom.

Take a dataset:

  data = [('male', 'homer' ('female', 'lisa', 'simpson'), ('' female  

then we have

  Gender first_name last_name 0 male Homer Simpson 1 woman via Simpson 2 male Bart Simpson 3 female Lisa Simpson 4 baby Maggie Simpson  

And there is a function to store results in a new column , I want to apply on every line.

 Title = '' 'if' gender == 'male': title = 'mister' alif gender == 'female': title = 'ms' if title == '': Return '+' Previous: Return Title + '' First [0] + 'Currently my method is: 

  People [' address'] = map (lambda line: get_address (* row) ), People .get_values ​​()) gender first_name last_name Address 0 Male Homer Simpson Mr. H. Simpson 1 woman by Simpson MS M. Simpson 2 Men Bart Simpson Mr. B. Simpson 3m Lit Lisa Simpson ms l. Simpson 4 Baby Maggie Simpson Maggi Simpson  

It works, but it is not elegant. It also feels bad to change to the unrelated list, then assigns back to a indexed column.

What are you applicable (func, axis = 1) this is your dataframe Will be applied according to a function line.

Modify the get_address of your method in your example ...

def get_address (line): # line index title = "" gender = line ['gender' ] Is a panda series with names as; #extract from panda series = gender first = line ['first_name'] #EgstrateFirstName from Pendes series # Extract Forrest = "[last_name"] # last name from pendas series = '' 'male' ': title =' mister 'alif gender == 'Woman': Title = 'MS' if Title == '': Return First + '' + Previous: Return Title + '' + [+] + "+ + Last + < P> then call people. Apply (get_address, axis = 1) which gives a new column (Actually it is a pandus series, correct sequence Add this code to add it to your dataframe, with this, how this is how dataframe knows how to add it as a column ...

  people ['address '] = People. Application (get_address), axis = 1)  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -