Python function that uses two different columns of a table - python

I have a table that has one column as day of birth and another as month of birth of students. I need to transform it into their zodiac signs.
I have found a function on the internet that transforms month and day of birth into zodiac signs, but it takes inputs. What if it's from two columns of a table?
def signo(table): #from https://www.w3resource.com/python-exercises/python-conditional-exercise-38.php
month = int(table.iloc[:,1])
day = int(table.iloc[:,2])
astro_sign = 0
if month == 12:
astro_sign = 'sagittarius' if (day < 22) else 'capricorn'
elif month == 1:
astro_sign = 'capricorn' if (day < 20) else 'aquarius'
elif month == 2:
astro_sign = 'aquarius' if (day < 19) else 'pisces'
elif month == 3:
astro_sign = 'pisces' if (day < 21) else 'aries'
elif month == 4:
astro_sign = 'aries' if (day < 20) else 'taurus'
elif month == 5:
astro_sign = 'taurus' if (day < 21) else 'gemini'
elif month == 6:
astro_sign = 'gemini' if (day < 21) else 'cancer'
elif month == 7:
astro_sign = 'cancer' if (day < 23) else 'leo'
elif month == 8:
astro_sign = 'leo' if (day < 23) else 'virgo'
elif month == 9:
astro_sign = 'virgo' if (day < 23) else 'libra'
elif month == 10:
astro_sign = 'libra' if (day < 23) else 'scorpio'
elif month == 11:
astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
return astro_sign
I tried using iloc or naming the column, but it does not work (and honestly, I don't know if it should work).
What I get when using iloc is:
NameError: ("name 'table' is not defined", 'occurred at index CO_CURSO')

Instead of trying to work with the entire table in your function signo, you can modify it to work on a single row of your DataFrame and then transform your entire dataset using apply:
First let's create some sample data:
import numpy as np
import pandas as pd
np.random.seed(28)
df = pd.DataFrame(
data={
"birth_month": np.random.randint(1, 12, 10),
"birth_day": np.random.randint(1, 30, 10)
}
)
print(df)
Output:
birth_month birth_day
0 2 24
1 10 13
2 6 13
3 7 6
4 5 9
5 1 25
6 4 19
7 8 12
8 1 29
9 4 25
Then we modify the function signo to work row by row:
def signo(row):
month = int(row[0])
day = int(row[1])
astro_sign = 0
if month == 12:
astro_sign = 'sagittarius' if (day < 22) else 'capricorn'
elif month == 1:
astro_sign = 'capricorn' if (day < 20) else 'aquarius'
elif month == 2:
astro_sign = 'aquarius' if (day < 19) else 'pisces'
elif month == 3:
astro_sign = 'pisces' if (day < 21) else 'aries'
elif month == 4:
astro_sign = 'aries' if (day < 20) else 'taurus'
elif month == 5:
astro_sign = 'taurus' if (day < 21) else 'gemini'
elif month == 6:
astro_sign = 'gemini' if (day < 21) else 'cancer'
elif month == 7:
astro_sign = 'cancer' if (day < 23) else 'leo'
elif month == 8:
astro_sign = 'leo' if (day < 23) else 'virgo'
elif month == 9:
astro_sign = 'virgo' if (day < 23) else 'libra'
elif month == 10:
astro_sign = 'libra' if (day < 23) else 'scorpio'
elif month == 11:
astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
return astro_sign
Finally apply the function and create a new column in the DataFrame:
df['sign'] = df.apply(signo, axis=1)
print(df)
Output:
birth_month birth_day sign
0 2 24 pisces
1 10 13 libra
2 6 13 gemini
3 7 6 cancer
4 5 9 taurus
5 1 25 aquarius
6 4 19 aries
7 8 12 leo
8 1 29 aquarius
9 4 25 taurus

I am assuming when you say table, you mean python dataframe.
You can iterate across each row using iterrows method
import pandas as pd
data = [{'month': 1, 'day': 10}, {'month': 4, 'day': 11},{'month': 5, 'day': 17},{'month': 11, 'day': 20}]
df = pd.DataFrame(data)
for index, row in df.iterrows():
month = row['month']
day = row['day']
And rest follows the logic to convert into zodiac signs

Related

Get the season when entering a month and day (python)

I am writing a code that allows the user to enter a month and date and the program will tell them what season it will be given the information that the user entered. What I currently have working is that when you enter certain months that have the season in all their days it will display the season. But when I enter a month and date that has 2 seasons it gives me an error. This is the season guide that I am following:
Spring: March 20 - June 20
Summer: June 21 - September 21
Autumn: September 22 - December 20
Winter: December 21 - March 19
What I am trying to figure out is why the program is not printing out stuff when certain dates are entered. For example if I enter March 25th it should print out spring but it doesnt.
This is my code:
input_month = input('Enter a month: ')
input_day = input('Enter a date: ')
month_lower = input_month.lower()
seasons = ()
if month_lower == 'january' or month_lower == 'febuary' or month_lower == 'march' and input_day <19:
seasons = 'winter'
elif month_lower == 'march' and input_day <=19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
seasons = 'spring'
elif month_lower == 'july' or month_lower == 'august' or month_lower == 'september' and input_day <22:
seasons = 'summer'
elif month_lower == 'october' or month_lower == 'november' or month_lower == 'december' and input_day <20:
seasons = 'autumn'
else:
print('Please enter a valid date')
print(seasons)
You have a problem in the spring condition, you should check if the day is strictly superior to 19:
elif month_lower == 'march' and input_day > 19 or month_lower == 'april' or month_lower == 'may' or month_lower == 'june' and input_day < 21:
seasons = 'spring'
I supposed you're getting input not an int error when entering date since you compare input_day to an int, quick fix for this is to change all input_day to int(input_day).
But if your problem is that march 25 not printing out what you want but instead gave out please enter valid date then that'd be because you've not gave it a condition just yet, all above condition are average less than 20 but none talk about 22-30

Python looping past values it shouldn't be [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
This is a follow up question (?) to a post I made yesterday: Python pandas df.loc not being found in a table
I'm trying to I'm trying to find a certain row and column in a given .csv file with pandas. Here is a snapshot of the table: Table
The table goes from 1/1/2015 to 12/31/2017. I've located the specific column and date I want to use from the csv file, and printing it so I can see if it's working properly. This is the code I have thus far:
months = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
month = str(input('Enter a month: '))
year = str(input('Enter a year: '))
if not (2015 <= float(year) <= 2017):
print('Enter a year bewteen 2015 and 2017, both included and try again')
exit()
day = 1
df1 = df.set_index(['Date'])
if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):
while day < 32:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
elif (month == 'April' or 'June' or 'September' or 'November'):
while day < 31:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
elif (month == 'February'):
if year == '2016':
while day < 29:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
else:
while day < 28:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
This code is working correctly for months with 31 days, but breaks for any other month. For example if I enter "June" for the month (no quotations), the code works until it reaches day 30, then tries to look for day 31. Why is this happening? It seems to be searching for day 31 in the date column even though I've restricted it to be less than 31.
Picture of error code:
Error
Your or statements are plain wrong.
if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):
always evaluates to true; you'll want to replace that idiom with e.g.
if month in ('January', 'March', 'May', 'July', 'August', 'October', 'December'):
It's as simple as changing your if and elif statements.
if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):
think of the or statement as another if: you have to compare it to the month again.
In other words: in your if statement, you're literally saying "if month is equal to January or if march or if May or if July ..." As we know, If 'July' really doesn't mean anything.
try : if month in ('January', 'March', 'May', 'July', 'August', 'October', 'December'):

Python for-loop, range counter [duplicate]

This question already has answers here:
How to get the last day of the month?
(44 answers)
Closed 4 years ago.
I have made a program that prints the each date of year. Is it possible to add a counter that will allow to limit the printable dates? I would print for example every 7 days. Thanks for any advice.
The original code below.
def main():
for month in range(1, 13):
daymax = 32
if month == 2:
daymax = 29
elif month == 4:
daymax = 31
elif month == 6:
daymax = 31
elif month == 9:
daymax = 31
elif month == 11:
daymax = 31
for day in range(1, daymax):
print(day,".", month,".", sep="")
main()
Just use another counter variable, and check if it's a multiple of 7.
def main():
counter = 0
for month in range(1, 13):
daymax = 32
if month == 2:
daymax = 29
elif month == 4:
daymax = 31
elif month == 6:
daymax = 31
elif month == 9:
daymax = 31
elif month == 11:
daymax = 31
for day in range(1, daymax):
if counter % 7 == 0:
print(day,".", month,".", sep="")
counter += 1
if day % 7 == 0
that should work. Try it.

how to get weekday from a given date in python?

2001-10-18
I want to calculate weekday e.g. Monday, Tuesday from the date given above. is it possible in python?
Here is one way to do this:
dt = '2001-10-18'
year, month, day = (int(x) for x in dt.split('-'))
answer = datetime.date(year, month, day).weekday()
There is the weekday() and isoweekday() methods for datetime objects.
Python doc
Here's what I have that got me if a leap year and also days in a given month (accounts for leap years). Finding out a specific day of the week, that I'm also stuck on.
def is_year_leap(year):
if (year & 4) == 0:
return True
if (year % 100) == 0:
return False
if (year % 400) == 0:
return True
return False
def days_in_month(year, month):
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
return 31
if month == 2:
if is_year_leap(year):
return 29
else:
return 28
if month == 4 or month == 6 or month == 9 or month == 11:
return 31

Project Euler #19, Python

I was solving Project Euler #19:
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
And here is the code :
months = { "January": 31,
"February" : 28,
"March" : 31,
"April" : 30,
"May" : 31,
"June" : 30,
"July" : 31,
"August" : 31,
"September" : 30,
"October" : 31,
"November" : 30,
"December" : 31}
def countingSundays():
day = 1
sunday_count = 0
for year in xrange(1901,2001):
for m in months:
day += months[m]
if year % 4 == 0 and m == "February":
day += 1
if day % 7 == 0:
sunday_count += 1
print "Sundays:", sunday_count
The output of the program is 172 which is incorrect.
I searched the answer to be 171.
So I wanted to know why am I getting the extra 1 Sunday ?
You're iterating over the months dict, expecting it to iterate in the order of the months, but dicts aren't ordered, so you can get the months in the wrong order.
Since you don't actually need the month names, you can just make months a list of the month lengths instead.
You should use the datetime library, which will handled all the leap year information automatically:
from datetime import date
from collections import Counter
counter = Counter()
for year in xrange(1901, 2001):
for month in xrange(1, 13):
day = date(year, month, 1)
counter[day.weekday()] += 1
print counter[6]
import time
from math import floor
"""
Gaussian algorithm to determine day of week
"""
def day_of_week(year, month, day):
"""
w = (d+floor(2.6*m-0.2)+y+floor(y/4)+floor(c/4)-2*c) mod 7
Y = year - 1 for January or February
Y = year for other months
d = day (1 to 31)
m = shifted month (March = 1, February = 12)
y = last two digits of Y
c = first two digits of Y
w = day of week (Sunday = 0, Saturday = 6)
"""
d = day
m = (month - 3) % 12 + 1
if m > 10: Y = year - 1
else: Y = year
y = Y % 100
c = (Y - (Y % 100)) / 100
w = (d + floor(2.6 * m - 0.2) + y + floor(y/4) + floor(c/4) - 2*c) % 7
return int(w)
"""
Compute the number of months starting on a given day of the week in a century
"""
def months_start_range(day,year_start,year_end):
total = 0
for year in range(year_start, year_end + 1):
for month in range(1,13):
if day_of_week(year, month, 1) == day: total += 1
return total
start = time.time()
total = months_start_range(0,1901,2000)
elapsed = time.time() - start
print("%s found in %s seconds") % (total,elapsed)
This might you solve the problem.
It took around 0.068 seconds to solve it.
Here is a different approach to tackle this question
public static void main(String[] args) {
int k = 0;
// String months[] = { "January", "February", "March", "April", "May", "June",
// "July", "August", "September",
// "October", "November", "December" };
String Days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int MonthsDdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int counter = 0;
for (int t = 1900; t <= 2000; t++) {
MonthsDdays[1]=28;
if (t % 4 == 0) {
if (t % 100 == 0)
{
if (t % 400 == 0)
MonthsDdays[1] = 29;
} else if (t % 100 != 0)
MonthsDdays[1] = 29;
}
int p = 0;
while (p < 12) {
for (int j = 0; j < MonthsDdays[p]; k++, j++) {
if (k == 7)
k = 0;
if (Days[k].equalsIgnoreCase("Sunday") && j == 0 && t > 1900) {
counter++;
}
}
p++;
}
}
System.out.println(counter);
}
I tried the Mathematical approach although we could use the calendar functions.
I first calculated the math of the months to determine the relationships between the first dates of the months using the other months. Also, for simplicity in calculating leap years, I calculated the year from March to Feb. If you want to calculate for the Jan and Feb of 1901, you can write a separate condition, and do the same to remove Jan and Feb of 2001. However, in this case, they do not really matter as they are not Sundays, so you could remove the last if condition for this specific case.
# Zero is Sunday and the rest of the days are according to mod7
# li stores the first days of the months in the year in every iteration
# The year in initial li is 1900 but the contents are Mar-1900 to Feb-1901
# At the end, we can check if Jan or Feb of 2001 contain a Sunday and remove if it does
li, cnt = [4,0,2,5,0,3,6,1,4,6,2,5], 0
# Could also initialize li from by the same method as below, but I had already calculated those
# cnt adds number of zeros in every iteration (the number of Sundays in every year) to its value
# As we don't count for the year 1900 cnt=0, else initialize cnt=li.count(0)
for year in range(1901,2001):
if year%4==0:
li[0]=li[8]=(li[11]+1)%7 #Set March and November to +1 value than last Feb
else:
li[0]=li[8]=li[11] #Set March and November to same value as last Feb
# The following values of other months will depend solely on their own March value
# You can check the Math if you want to
li[3]=li[11]=(li[0]+1)%7;li[6]=li[9]=(li[0]+2)%7;li[1]=li[4]=(li[0]+3)%7;li[2]=li[10]=(li[0]-2)%7;li[5]=(li[0]-1)%7;li[7]=(li[0]-3)%7
cnt = cnt + li.count(0)
# This is to remove the extra two months of the year 2001 if they bother the answer
if li[10] == 0 or li[11] == 0:
cnt = cnt-1
print(cnt)
This was my first answer on StackOverflow, I hope I wrote well. ;-)
The mistakes you have:
The way you calculate leap years
Dictionary does not keep the order necessarily
You assume January 1st is Sunday
The correct program would be:
from collections import OrderedDict
months = OrderedDict( [("January",31),("February", 28),("March",31),
("April", 30), ("May", 31), ("June", 30),
("July", 31), ("August", 31), ("September", 30),
("October", 31), ("November", 30), ("December", 31)] )
days = ['Tuesday','Wednesday', 'Thursday','Friday','Saturday', 'Sunday', 'Monday']
day = 0
sunday_count = 0
def isLeap(year): #https://en.wikipedia.org/wiki/Leap_year#Algorithm
leap = True
if year % 4 != 0:
leap = False
elif year % 100 != 0:
leap = True
elif year % 400 != 0:
leap = False
return leap
for year in xrange(1901,2001):
leap = isLeap(year)
for m in months:
dayName = days[day%7]
if dayName == "Sunday":
sunday_count += 1
#print year, m, dayName
day += months[m]
if leap == True and m == "February":
day += 1
print sunday_count
# print 171
Also, some days:
1901 January Tuesday
1901 February Friday
1901 March Friday
1901 April Monday
1901 May Wednesday
1901 June Saturday
1901 July Monday
1901 August Thursday
1901 September Sunday
...
import pandas as pd
from datetime import date
start = date(1901, 1, 1)
end = date(2000, 12, 31)
d = pd.date_range(start, end, freq='MS').strftime('%A')
s = pd.Series(d)
print(s.value_counts())
So I approached this problem from not a date perspective but of a counting days.
Here's my solution:.
days_1st = list()
day_counter = 1
for year in range(1900, 2001):
for month in range(1,13):
#Skip for year 1900 as count starts from 1901, but this still
#adds the days hence keeping the cycle in sync!
if year != 1900:
days_1st.append(day_counter)
if month == 4 or month == 6 or month == 9 or month == 11:
day_counter+=30
elif month == 2 and ((year % 100 == 0 and year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)):
day_counter+=29
elif month == 2:
day_counter+=28
else:
day_counter+=31
# mod 7 because since the day the counting started (1 Jan 1900 -
# Monday) Every 7th day is a sunday!
days_sunday = list(filter(lambda x: x % 7 == 0, days_1st))
print(len(days_sunday))
A = [31,28,31,30,31,30,31,31,30,31,30,31]
sunday =0
gK = 1
for y in range(1901,2001):
if(y %4 ==0):
A[1] = 29
else:
A[1] = 28
for m in range(len(A)):
for d in range(1,A[m]+1):
if(gK ==6):
if(d==1):
sunday +=1
gK =0
else:
gK =gK+1
print(sunday)
==>Solution in python
euler19.py
normal_year = [31,28,31,30,31,30,31,31,30,31,30,31]
leap_year = [31,29,31,30,31,30,31,31,30,31,30,31]
years = [ normal_year ] * 100
for i in range(3, len(years), 4) :
years[i] = leap_year
current_day = (0+365) % 7
sundays = 0
for y in years :
for m in y :
if current_day % 7 == 6:
sundays += 1
current_day += m%7
print (sundays)
I think I got the answer. I am not sure though.. your logic was right. But needed a little improvement. We need to start off by counting the number of Tuesdays first as we clearly know that it was Monday on Jan 1, 1900.
months = { "January": 31,
"February" : 28,
"March" : 31,
"April" : 30,
"May" : 31,
"June" : 30,
"July" : 31,
"August" : 31,
"September" : 30,
"October" : 31,
"November" : 30,
"December" : 31}
for month in months:
print(months[month])
tuesday_count = 0
day = 0
extra_days = 0
for year in range(1901, 2001):
days_in_the_year = 0
for month in months:
day += months[month]
days_in_the_year += months[month]
if( year % 4 == 0 and month == 'February'):
if (year % 100 != 0):
extra_days += 1
days_in_the_year += 1
day += 1
elif(year % 100 ==0 and year % 400 ==0):
extra_days += 1
days_in_the_year += 1
day += 1
if( (day) % 7 == 0):
tuesday_count += 1
print('No. of days in the year',year,'are',days_in_the_year)
print('No. of Tuesdays counted so far is =', tuesday_count)
print('The number of extra_days because of the leap years are:',extra_days)
# print('extra_days % 7 =', '25 % 7 =', extra_days % 7)
print('So, there were', extra_days // 7, 'extra_no_of_weeks left that we haven\'t considered. After that, it\'s followed by --wed, thu, fri and sat (we don\'t need to consider that).\n So, the total number of Tuesdays are', tuesday_count+3 )
tuesday_count += 3
print('This means only 2 Sundays that have followed')
sunday_count = tuesday_count - 1
print('Since, 1901 Jan1 would be a Tuesday, we need to subract one from the total number of Sundays\n So, the total number of sundays are:', )
sunday_count=sunday_count-1
print(sunday_count)
months=[31,28,31,30,31,30,31,31,30,31,30,31]
leap=[31,29,31,30,31,30,31,31,30,31,30,31]
sundays=0
start=2
for y in range(25):
for nonleap in range (3):
for j in months:
start=(start+j)%7
if start == 0:
sundays+=1
for m in leap:
start=(start+m)%7
if start == 0:
sundays+=1
print sundays
Note that the problem defines the first day of 1900 as Monday and you define the first day of 1901 as Monday.
months = [31,28,31,30,31,30,31,31,30,31,30,31]
def countingSundays():
day = 1
sunday_count = 0
for year in range(1900,1901):
for m in months:
day += m
if (year % 4 == 0 and m == 28):
day += 1
for year in range(1901,2001):
for m in months:
day += m
if (year % 4 == 0 and m == 28):
day += 1
if day % 7 == 0:
sunday_count += 1
return sunday_count
print ("Sundays:", countingSundays())
you have initialized the day variable to 1 but the 1st Jan 1901 is a Tuesday. I made the same error ;-)

Categories