Search code examples
pythonpandaslistdataframelist-comprehension

How to do list comprehension with Dataframe when using scalar operation?


I have a list of dataframes. All the dataframes' dtype is <date, int>. I want to multiply 100 with the int column only. So I did -

dfs = [df1, df2, ...]
dfs = [df.select_dtypes(include=['number']) * 100 for df in dfs]

This is dropping the first column from every dataframe. How to keep that intact?


Solution

  • This is because select_dtypes only return a subset of the DataFrame’s columns based on the column dtypes. The easiest way I can think of this to exclude the number dtypes to get the remaining columns and merge it to get the original DataFrame.

    [
        pd.concat(
            [
                df.select_dtypes(include=["number"]) * 100,
                df.select_dtypes(exclude=["number"]),
            ],
            axis=1,
        )
        for df in dfs
    ]