Search code examples
pythonpandasdataframemultiple-columnsmultiplication

Multiply columns with both integers and strings


I have a large DataFrame with 50+ columns. I want to multiply every column by another column, however some strings exist within the data.

df[df.loc[:, df.columns != "Total Balance"].columns].multiply(df["Total Balance"], axis="index")

The df.loc[:, df.columns != "Total Balance" operation is so that I dont multiply the Total Balance by itself.

My issue is that some of the elements in the DataFrame are strings, thuus I get the error:

TypeError: can't multiply sequence by non-int of type 'float'

When an integer is multiplied by a string, I want the Dataframe to fill that spot with NaN. I have checked the docs and there is no built in default argument. Any suggestions?


Solution

  • Add this to coerce your dataframe into a numeric table with strings replaced by NaN (unless they're strings that can be converted to a numeric like "5.0")

    df = df.apply(pd.to_numeric, errors='coerce')
    

    Then do whatever you need.