I have been trying to use a script to read in data and compute the average frequency of use per year from the table (AlcData) below:
Frequency |
---|
Never |
1-2 times per year |
3-4 times per month |
Every day |
1-2 times per week |
To this table:
Frequency |
---|
0 |
1.5 |
42 |
365 |
0 |
78 |
0 |
As you can see there are some missing values that are read as "NaN" in the first table that I would like to convert to the value 0. I have tried dozens of solutions online to try and replace (or less ideally drop) the NaN values but I keep getting the error: "cannot convert float NaN to integer". Does anyone know where my error may be in the code below?
import pandas as pd
AlcData['Frequency'].replace(str('Every day'), str('365 days per year'))
AlcData['Frequency'].replace(str('Never'), str('0 days per year'))
AlcData["Frequency"].fillna(0)
AlcData['Frequency'].str.split().str[-1].map({'week': 52, 'month': 12, 'year': 1}).mul(AlcData['Frequency'].str.extract(r'(\d )\D*(\d )?').ffill(axis=1).astype(int).mean(axis=1))
Any tips at all would be greatly appreciated!
CodePudding user response:
df['Frequency'].replace(str('Every day'), str('365 days per year'),inplace=True)
df['Frequency'].replace(str('Never'), str('0 days per year'), inplace=True)
df['Frequency'] = df['Frequency'].str.split().str[-1].map({'week': 52, 'month': 12, 'year': 1}).mul(df['Frequency']
.str.extract(r'(\d )\D*(\d )?').ffill(axis=1).astype(float).mean(axis=1))
df["Frequency"].replace(np.nan, 0, inplace=True)