When you use apply
, it's implicitly passing each record of per_country['type']
to func. If you want to do this more simply and clearer, you can use a lambda function.
per_country['virus_confirmed'] = per_country.apply(lambda x: x['cases'] if x['type']=="confirmed" else 0, axis=1)
EDIT:
For the apply function, note that you're applying it to a series in the DataFrame. This means that x is each record from that column and that you don't need to specify the axis. I applied it to the whole DataFrame, which means I do need to specify the axis. With axis=1, x in my lambda function stands in for each row of the dataframe.
Lastly, I also forgot to mention that you can get this done even quicker with the dummy variables by doing:
per_country['virus_confirmed'] = per_country['cases']*per_country['confirmed']
Because columns that aren't confirmed are marked with a 0, it will give you the same result, but in a vectorized manner. If you're working with a Covid-19 dataset, I don't think you'll notice a huge difference, but it's a good habit to look for opportunities to vectorize.