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?
CodePudding user response:
For this case, it might be better to use a for loop to alter the dataframes.
for i in range(len(dfs)):
dfs[i].select_dtypes(include=['number']) = dfs[i].select_dtypes(include=['number']) * 100
The problem with the list comprehension is that you are only selecting the columns that have dtype 'number' on the RHS, so that is what your code assigns the value to. If you really want to use list comprehension, then you could use something like this:
dfs = [pd.concat([
select_dtypes(exclude=['number']),
select_dtypes(include=['number']) * 100
], axis=1) for df in dfs]
CodePudding user response:
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 ]