Pandas: How can I use the apply () function for a single column?

I have a pandas data frame with two columns. I need to change the values ​​of the first column without affecting the second, and return the entire data frame with only the values ​​of the first column changed. How can I do this using pandas?

+162
python pandas dataframe
Jan 23 '16 at 10:04 on
source share
4 answers

For sample df data like:

 a,b 1,2 2,3 3,4 4,5 

What would you like:

 df['a'] = df['a'].apply(lambda x: x + 1) 

which returns:

  ab 0 2 2 1 3 3 2 4 4 3 5 5 
+239
Jan 23 '16 at 10:15
source share

You don't need a function at all. You can work with the whole column directly.

Sample data:

 >>> df = pd.DataFrame({'a': [100, 1000], 'b': [200, 2000], 'c': [300, 3000]}) >>> df abc 0 100 200 300 1 1000 2000 3000 

Half of all values ​​in column a :

 >>> df.a = df.a / 2 >>> df abc 0 50 200 300 1 500 2000 3000 
+34
Jan 23 '16 at 10:58 on
source share

For a single column, it is better to use map() , for example:

 df = pd.DataFrame([{'a': 15, 'b': 15, 'c': 5}, {'a': 20, 'b': 10, 'c': 7}, {'a': 25, 'b': 30, 'c': 9}]) abc 0 15 15 5 1 20 10 7 2 25 30 9 df['a'] = df['a'].map(lambda a: a / 2.) abc 0 7.5 15 5 1 10.0 10 7 2 12.5 30 9 
+32
Jan 23 '16 at 10:49 on
source share

Although the answers given are correct, they change the initial data frame, which is not always desirable (and given that the OP asked for examples β€œusing apply ”, they probably need a version that returns a new data frame, as apply does).

This is possible using the assign command: this is valid for assign existing columns, as the documentation reads (highlighted mine):

Assign new columns to the DataFrame.

Returns a new object with all source columns in addition to new ones. Existing columns to be reassigned will be overwritten .

In short:

 In [1]: import pandas as pd In [2]: df = pd.DataFrame([{'a': 15, 'b': 15, 'c': 5}, {'a': 20, 'b': 10, 'c': 7}, {'a': 25, 'b': 30, 'c': 9}]) In [3]: df.assign(a=lambda df: df.a / 2) Out[3]: abc 0 7.5 15 5 1 10.0 10 7 2 12.5 30 9 In [4]: df Out[4]: abc 0 15 15 5 1 20 10 7 2 25 30 9 
+1
Jun 26 '19 at 8:02
source share



All Articles