Finding the product of all columns in a dataframe is easy:
df['Product'] = df.product(axis=1)
How can I specify which column names (not column numbers) to include in the product operation?
From the help page for DataFrame.product(), I am not sure whether it is possible.
You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.
Pandas DataFrame explode() Method The explode() method converts each element of the specified column(s) into a row.
The mul() method of the pandas Series multiplies the elements of one pandas Series with another pandas Series returning a new Series. Multiplying of two pandas. Series objects can be done through applying the multiplication operator “*” as well.
You can use the df[[colname1, colname2, colname3...]]
syntax to select the columns you want and then call .product
on that:
>>> df = pd.DataFrame({"A": [2,2], "B": [3,3], "C": [5,5]})
>>> df
A B C
0 2 3 5
1 2 3 5
[2 rows x 3 columns]
>>> df[["A", "C"]].product(axis=1)
0 10
1 10
dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With