python - Return output of function that takes pandas dataframe as a parameter -


I have a panda dataframe looks like that:

  d = { 'some_col': [A, B, B, C, D, E]  

Much work in my work requires only one work in Pondo. I am starting to write standardized functions which take dataframe as a parameter and some will return.

  def alert_read_text (df, alert_status = None): Here's a simple one that (alert_status No): print "should be referred to a column name with warning alerts' alert_read_criteria = Df [alert_status] & gt; = 1 df [alert_status] .loc [alert_read_criteria] = 1 alert_status_dict = {0: 'read', 1 'Read'} df [alert_status] = df [alert_status] .map (alert_status_dict) return df [Alert_status]  

In this way, one can add a column to an existing data frame, like this:

  df ['] alert_status_text '] = alert_read_text (df, alert_status =' alert_status')  

However, at present, this function will return correctly a series, but will also modify existing columns. How do you make it so that the original column has been passed? Does not modify?

As you have found, the data passed in the data frame will be modified because the parameter is passed by reference It is true in Python and there is nothing to do with Pando.

So if you do not want to modify passed DF CC:

  def alert_read_text (df, alert_status = None): print "a column with warnings :: alerts Name must be specified 'if (alert_status is none) per = df.copy () alert_read_criteria = copy [alert_status] & gt; = 1 copy [Alert_status] Kloc [Alert_read_criteria] = 1 Alert_status_dict = {0: 'read', 1 'Read'} copy [alert_status] = copy [alert_status] .map (alert_status_dict) returns a copy [alert_status]  

Also:


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 -