Python: Explorative Data Analysis, Replacing Nan. Help! - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Python: Explorative Data Analysis, Replacing Nan. Help! (541 Views)
| Python: Explorative Data Analysis, Replacing Nan. Help! by DasaintHope(op): 12:42pm On Nov 20, 2022 |
Good day I am new to data analysis and python I want to replace all NaN value in titanic dataset, specifically the age column. I created a function: def impute_age(cols): age = cols[0] pclass == cols[1] if pd.isnull(age): if pclass == 1 return 37 elif pclass == 2: return 29 else: return 24 else: return age. The output goes: Syntax Error: invalid syntax. it indicated the last else was the issue. The video I am following the tutor did exactly this and had no error. I would appreciate your input. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by Samuell2019(m): 4:58pm On Nov 20, 2022 |
DasaintHope:The second to the last else should be elif instead. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by DasaintHope(op): 6:25pm On Nov 20, 2022*. Modified: 9:31pm On Nov 20, 2022 |
Samuell2019:Thanks man. modified: @Samuell2019 the problem is still persisting. it says the elif is incorrect syntax. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by semmyk(m): 6:38pm On Nov 20, 2022 |
Glancing through, seems you might have multiple error down the line. First step, kindly check your indentations Something like below. I'll try run with random value and feedback. Updated: I see NL distorted the indents. I've manually insert "." to try and preserve indents. Kindly remove all the "." Updated: I see NL continues to distort. I'll do a screenshot def impute_age(cols): age = cols[0] # assign first column to age Series pclass = cols[1] # why compare operator instead of assign | pclass == cols[1] if pd.isnull(age): if pclass == 1: #integer return 37 elif pclass == 2: #integer return 29 else: return 24 else: return age impute_age(cols_test) DasaintHope:
|
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by DasaintHope(op): 7:02pm On Nov 20, 2022*. Modified: 9:29pm On Nov 20, 2022 |
semmyk:Thanks boss! If I understand correctly the issue is the = I put instead of == right? Modified: Boss the problem still persists. The issue is with the last else statement. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by semmyk(m): 9:54pm On Nov 20, 2022 |
ok. Noted sir. If you could provide the exact error message. That will help. Typically, that's the easiest way to assist. While at it, kindly take note that a code might not throw an error, but might still have a functional error. essentially, it runs but the outcome is not the intended. In your case, the positioning of elif, else goes a long way in what the code would do. See, for instance, the snapshot below. NB: this is based on assumption and generated data (which will be different from that of your trainer)! DasaintHope:In between, I'm not sure why the trainer used the example (s)he used. Not so sure it's pythonic enough.
|
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by semmyk(m): 10:05pm On Nov 20, 2022 |
@DasaintHope [update] Intuitively, I just checked online now and realised that what you're working on (the Titanic dataset) apparently has many examples. These two might be of assistance. PS: going by them, then my assumption in my worked example is in line with what I assumed them to be. NB: I could have even opted for np.where() https://python-forum.io/thread-2758.html https://www.kaggle.com/code/frtgnn/a-simple-guide-to-titanic-survival-classifier/notebook All the best. Enjoy Python coding and ML the pythonic way. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by DasaintHope(op): 10:30pm On Nov 20, 2022 |
semmyk:Thanks bro I've gotten it. I shouldn't have made the indentation same for both else statements. |
| Re: Python: Explorative Data Analysis, Replacing Nan. Help! by semmyk(m): 8:13am On Nov 21, 2022 |
semmyk:So, I gave np.where a shot just now. Trick seems to be nested np.where (NB: two 'cascaded' np.where gave a wired output). NB: NL doesn't preserve indentations. NB: I used mybinder online from my tab. ## np.where | https://numpy.org/doc/stable/reference/generated/numpy.where.html
|
Nairaland's February Front Page Data Analysis • Applied Statistics For Data Analysis/science • Programming Not Enough Anymore: Data Analysis, Ml And A.i Is The Future. • 2 • 3 • 4
Top 10 Best Coding Languages For Kids To Learn In 2023 • Will You Pay To Use This? • I Want To Learn This