I have a dataframe in which I have categorical as well as numerical columns.
data = [['A',"India",10,20,30,15,"Cochin"],['B',"India",10,20,30,40,"Chennai"],['C',"India",10,20,30,15,"Chennai"]]
df = pd.DataFrame(data,columns=['Product','Country',"2016 Total","2017 Total","2018 Total","2019 Total","Region"])
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
I know what will be the names of the column of numerical variables(which need to be captured dynamically):
start_year = 2016
current_year = datetime.datetime.now().year
previous_year = current_year - 1
print(current_year)
year_list = np.arange(start_year, current_year+1, 1)
cols_list = []
for i in year_list:
if i <= current_year:
cols = str(i)+" Total"
cols_list.append(cols)
cols_list
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
I am trying to identify if the values in the columns of cols_list when multiplied is negative or not
How this can be done in pandas? I am not able to figure out how to loop through the cols_list and pull the columns from dataframe and multiply
Expected output:
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region Negative
0 A India 10 20 30 15 Cochin No
1 B India 10 20 30 40 Chennai No
2 C India 10 20 30 15 Chennai No