Oct-25-2020, 12:32 PM
(Oct-25-2020, 09:52 AM)snippsat Wrote: Also when using Pandas is looping through rows/columns rarely the best solution.
In fact Pandas even has ared warning
about this.
Pandas Wrote:Iterating through pandas objects is generally slow.So as example with your code,could eg use
In many cases, iterating manually over the rows is not needed and can be avoided (using) avectorized solution
,
many operations can be performed using built-in methods or NumPy functions, (boolean) indexing.np.where
.
import pandas as pd import numpy as np numbers = pd.DataFrame([[3],[8],[4],[9],[14],[17]], columns=['Numbers']) numbers['Odd/Even'] = np.where(numbers['Numbers'] % 2, 'Odd', 'Even')
Output:>>> numbers Numbers Odd/Even 0 3 Odd 1 8 Even 2 4 Even 3 9 Odd 4 14 Even 5 17 Odd
Thanks for answering but we didn't give students the numpy, is there other ways?