pandas transform

>>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
>>> df
   A  B
0  0  1
1  1  2
2  2  3
>>> df.transform(lambda x: x + 1)
   A  B
0  1  2
1  2  3
2  3  4

Here is what the above code is Doing:
1. Create a DataFrame with two columns, A and B.
2. Apply a transformation function to each column.
3. The transformation function is just a lambda function that adds 1 to each element.
4. The result is a new DataFrame with transformed values.

Note that you can also apply a transformation function to a single column. For example, let’s say we want to add 1 to column A but not column B. We can do this by passing in the name of the column as a string:

>>> df[‘A’].transform(lambda x: x + 1)
0 1
1 2
2 3
Name: A, dtype: int64

You can also use the apply() method to apply a function to a DataFrame. The apply() method takes a function as an argument and applies it to each column or row in the DataFrame, depending on the axis argument. By default, the axis argument is 0, which means that the function will be applied to each column. If we set the axis argument to 1, the function will be applied to each row.

For example, let’s say we want to find the sum of each row in our DataFrame. We can do this by setting the axis argument to 1:

>>> df.apply(lambda x: x.sum(), axis=1)
0 1
1 3
2 5
dtype: int64

As you can see, the function is applied to each row and the result is a Series with the sum of each row.

You can also use the apply() method to apply a function to a single column. For example, let’s say we want to find the sum of column A. We can do this by passing in the name of the column as a string:

>>> df[‘A’].apply(lambda x: x.sum())
0 0
1 1
2 2
Name: A, dtype: int64

Note that you can also use the transform() method to apply a function to a single column. The transform() method is faster than the apply() method, so it’s generally preferred.

You can also use the apply() method to apply a function to multiple columns. For example, let’s say we want to find the sum of columns A and B. We can do this by passing