![]() |
Using pandas library - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using pandas library (/thread-30540.html) |
Using pandas library - ramitaherwahdan1978 - Oct-25-2020 Hi, I am a teacher and we are using python and pandas data structure. I am having trouble with this code: import pandas as pd numbers = pd.DataFrame([[3],[8],[4],[9],[14],[17]]) numbers.index = ["Odd", "Even", "Even", "Odd", "Even", "Odd"] # check if 0 or 1 for i in numbers: i = numbers % 2 print(i) if i == 0: numbers[1] = ["Y"] else: numbers[1] = ["N"]I get i printed correctly but when i run the if statements and trying to add new column data i get this error: Quote:Traceback (most recent call last): Please help, Thanks. RE: Using pandas library - bowlofred - Oct-25-2020 You're overwriting the for loop variable i within the loop. You never use i as an index into the dataframe.
RE: Using pandas library - snippsat - Oct-25-2020 Also when using Pandas is looping through rows/columns rarely the best solution. In fact Pandas even has a red warning about this. Pandas Wrote:Iterating through pandas objects is generally slow.So as example with your code,could eg use 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')
RE: Using pandas library - ramitaherwahdan1978 - Oct-25-2020 (Oct-25-2020, 09:52 AM)snippsat Wrote: Also when using Pandas is looping through rows/columns rarely the best solution. Thanks for answering but we didn't give students the numpy, is there other ways? RE: Using pandas library - jefsummers - Oct-25-2020 My understanding is that Pandas is built on Numpy (based on a google search and checking a number of sites), so if you gave them Pandas you also gave them numpy implicitly. Try it out RE: Using pandas library - jefsummers - Oct-25-2020 Did figure out something that does not use numpy. import pandas as pd numbers = pd.DataFrame([[3],[8],[4],[9],[14],[17]], columns=['Numbers']) numbers['Odd/Even'] = numbers['Numbers'].map(lambda x : x%2) numbers
|