Home > Software design >  Apply a condition to every column in a dataframe except a list of some string columns
Apply a condition to every column in a dataframe except a list of some string columns

Time:07-18

I am trying to apply the following calculation in all the columns of a dataframe EXCEPT a list of 3 string columns. The issue is that although the code bellow works fine based on the sample data, in reality the Month columns are upwards of 100 and they are getting increased every month while the 3 string columns are fix. The months list should contain 100 columns which they will be 1 every month so I want to just apply the /100 on all the columns that the View description == 'Percent change' except the Series ID, View Description and Country columns. How can I modify the list so that it includes just the 3 string columns and the .loc is applied to everything else.

import pandas as pd

df = pd.DataFrame({
    'Series ID': ['Food', 'Drinks', 'Food at Home'],
    'View Description': ['Percent change', 'Original Data Value', 'Original Data Value'],
    'Jan': [219.98, 'B', 'A'],
    'Feb': [210.98, 'B', 'A'],
    'Mar': [205, 'B', 'A'],
    'Apr': [202, 'B', 'A'],
    'Country': ['Italy', 'B', 'A']
})

months = ['Jan', 'Feb', 'Mar', 'Apr']
df.loc[df['View Description'] == 'Percent change', months] /= 100
print(df)

Thanks!

CodePudding user response:

You can change months to be a boolean array which omits the string columns:

months = ~df.columns.isin(['Series ID', 'View Description', 'Country'])

The command for applying the division will be the same as you have above. This change just programmatically selects the month columns by excluding the non-month columns.

  • Related