To create a new column in a DataFrame that is the mean of specific columns in a given row, you can use the mean
function from the pandas library in Python.
Here's an example to demonstrate this:
main.py295 chars14 lines
By using the square brackets []
, you can specify the column names for which you want to calculate the mean. In the example above, we calculate the mean of columns 'A' and 'B' for each row and assign the result to a new column named 'Mean'. The mean
function is applied along the row axis (axis=1
).
Output:
main.py80 chars5 lines
In this case, the 'Mean' column is computed by taking the mean of the 'A' and 'B' columns for each row.
gistlibby LogSnag