Converting Yes to 1 and No to 0 in python - python

I have a csv file few columns are as Yes/No's I have tried most of the solutions on stack over flow nothing seems to be working.
sample['colval'] = sample['colval'].apply(lambda x: 0 if x=='N' else 1)
For example, I used the above code on my data. It converted everything to 1's, as in Y to 1 and N to 1.
So many other examples too, all yielded similar results, few resulting in 'None' as output for Y/N.
sample.colval[0:20]
0 N
1 N
2 N
3 N
4 N
5 N
6 N
7 N
8 N
9 N
10 N
11 Y
12 N
13 N
14 N
15 N
16 N
17 N
18 Y
19 N
Name: colval, dtype: object
Please help, Thank you

Found it difficult to repeat given the older version of Python and no access to the original data but your output above suggests whitespace in with the value. Try
sample['colval'] = sample['colval'].apply(lambda x: 0 if x.strip()=='N' else 1)

Related

Why am I getting different results from the same python code for the same input data?

I wrote some code for a cp contest and I have this piece of code:
t=int(input())
for _ in range(t):
solve()
I got 2 different outputs for the same data when a) it is the only input and b) when it was the final input out of the 3 test cases. The same .py file gives different outputs for the same input and I'm completely lost. Any idea why this is happening?
Thanks in advance :D
The question was:
You are given a tree with N nodes. The i-th node has value a[i]. Recall that a tree is a connected graph with N-1 edges. When you remove any one edge from a tree, you get two trees.
Let f(T) be a function that takes input a tree T and returns the maximum value among all nodes in that tree. More formally, if the i-th node has value b[i] then f(T) = max({b[1], b[2], ..., b[k-1], b[k]}) where k is the number of nodes in the tree.
Let us take a random edge that exists in the tree and remove it. Let us call the two trees formed as T1 and T2.
Find the expected value of |f(T1) - f(T2)| modulo 10^9 + 7. Note that each edge has equal probability of being selected.
The code:
from math import gcd
MOD=(10**9+7)
def inv(a,m):
m0=m
y=0
x=1
if m==1:
return 0
while a>1:
q=a//m
t=m
m=a%m
a=t
t=y
y=x-q*y
x=t
if x<0:
x=x+m0
return x
def dfs_update_max(graph,source,values,visited=[]):
if source not in visited:
visited.append(source)
if not graph[source]:
return
for neighbour in graph[source]:
if(neighbour not in visited):
print(f"edge {source}--{neighbour}")
dfs_update_max(graph,neighbour,values,visited)
values[source-1]=max(values[neighbour-1],values[source-1])
print(f"back {neighbour}--{source} value at {source} updated as max of node values at {neighbour} and {source}={values[source-1]}")
print(values)
def solve():
n=int(input())
g={}
e=[]
for i in range(1,n+1):
g[i]=[]
for i in range(n-1):
u,v=[int(i) for i in input().split()]
g[u].append(v)
g[v].append(u)
e.append([u-1,v-1])
values=[int(i) for i in input().split()]
max=values[0]
max_index=0
for i in range(1,n):
if(values[i]>max):
max=values[i]
max_index=i
dfs_update_max(g,max_index+1,values)
num=0
for i,j in e:
num+=values[max_index]-min(values[i],values[j])
den=n-1
print(values, "outside")
x=gcd(num,den)
p=num//x
q=den//x
q_=inv(q,MOD)
print(p*q_%MOD)
t=int(input())
for _ in range(t):
solve()
Input format:
First line contains an integer T denoting number of test cases. Then T test cases follow.
First line of each test case contains a single integer N denoting number of nodes in the tree.
Next N-1 lines contains 2 integers each u,v describing an edge between node u and v. It is guaranteed that these edges form a tree.
The next line contains N integers a[1], a[2], ..., a[N]
file 1:
3
3
1 2
1 3
1 5 5
3
1 2
1 3
5 1 5
10
4 6
4 1
4 7
6 5
7 9
5 8
9 3
1 2
7 10
15 23 90 4 29 44 105 30 27 81
file 2:
1
10
4 6
4 1
4 7
6 5
7 9
5 8
9 3
1 2
7 10
15 23 90 4 29 44 105 30 27 81

Table of Squares and Cubes

I am learning Python on my own and I am stuck on a problem from a book I purchased. I can only seem to get the numbers correctly, but I cannot get the title. I was wondering if this has anything to do with the format method to make it look better? This is what I have so far:
number = 0
square = 0
cube = 0
for number in range(0, 6):
square = number * number
cube = number * number * number
print(number, square, cube)
What I am returning:
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
I would like my desired output to be this:
number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
Here we can specify the width of the digits using format in paranthesis
print('number square cube')
for x in range(0, 6):
print('{0:6d}\t {1:7d}\t {2:3d}'.format(x, x*x, x*x*x))
This would result in
number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
You need to print the header row. I used tabs \t to space the numbers our properly and f-stings because they are awesome (look them up).
number = 0
square = 0
cube = 0
# print the header
print('number\tsquare\tcube')
for number in range(0, 6):
square = number * number
cube = number * number * number
# print the rows using f-strings
print(f'{number}\t{square}\t{cube}')
Output:
number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
The only thing this doesn't do is right-align the columns, you'd have to write some custom printing function for that which determines the proper width in spaces of the columns based on each item in that column. To be honest, the output here doesn't make ANY difference and I'd focus on the utility of your code rather than what it looks like when printing to a terminal.
There is a somewhat more terse method you might consider that also uses format, as you guessed. I think this is worth learning because the Format Specification Mini-Language can be useful. Combined with f-stings, you go from eight lines of code to 3.
print('number\tsquare\tcube')
for number in range(0, 6):
print(f'{number:>6}{number**2:>8}{number**3:>6}')
The numbers here (e.g.:>6) aren't special but are just there to get you the output you desired. The > however is, forcing the number to be right-aligned within the space available.

What's the cleanest way to print an equally-spaced list in python?

Please close if this is a duplicate, but this answer does not answer my question as I would like to print a list, not elements from a list.
For example, the below does not work:
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(%3s % mylist)
Desired output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Basically, if all items in the list are n digits or less, equal spacing would give each item n+1 spots in the printout. Like setw in c++. Assume n is known.
If I have missed a similar SO question, feel free to vote to close.
You can exploit formatting as in the example below. If you really need the square braces then you will have to fiddle a bit
lst = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
frmt = "{:>3}"*len(lst)
print(frmt.format(*lst))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
items=range(10)
''.join(f'{x:3}' for x in items)
' 0 1 2 3 4 5 6 7 8 9'
If none of the other answers work, try this code:
output = ''
space = ''
output += str(list[0])
for spacecount in range(spacing):
space += spacecharacter
for listnum in range(1, len(list)):
output += space
output += str(list[listnum])
print(output)
I think this is the best yet, as it allows you to manipulate list as you wish. even numerically.
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(*map(lambda x: str(x)+" ",a))

How do I draw this tree pattern in python?

Given a height 1<=h<=15, how do I go about drawing this tree? I'll need to be able to traverse it later to solve some questions.
For h = 1, just the root labeled 1.
For h = 2,
3
1 2
For h = 3,
7
3 6
1 2 4 5
etc.
All that really strikes so far has been trying to find a relation from the top-down (for the left side tree, the left node will be (parent-1)/2 and the right child is parent-1 but this isn't a consistent pattern), but I can't seem to find anything . Since i need to be able to generate the tree, I'm not sure how to use a heap-structure either. I'm not sure where to start on recursion either. Any ideas are welcome.
Your tree could be drawn recursively, but here is non-recursive code.
def ruler(n):
result = 1
while not (n & 1):
n >>= 1
result += 1
return result
def printtree(h):
widthofnum = len(str(2**h - 1))
for row in range(h, 0, -1):
maxcol = 2**(h - row)
width = 2**(row-1) * (widthofnum + 1) - 1
valincr = 2**row - 2
val = valincr + 1
for col in range(1, maxcol + 1):
print(str(val).center(width), end=' ')
val += ruler(col) + valincr
print()
printtree(3)
That prints
7
3 6
1 2 4 5
while printtree(5) gives
31
15 30
7 14 22 29
3 6 10 13 18 21 25 28
1 2 4 5 8 9 11 12 16 17 19 20 23 24 26 27
The spacing may not be ideal for larger numbers, but it works. Note that each line ends in at least one space, for code simplicity. The print statement means this is Python 3.x code. This non-recursive code lets us print from top-to-bottom, left-to-right without any backtracking or storage of strings. However, that complicates the calculations involved.
One key to that code is the discrete ruler function, which can be defined as "the exponent of the largest power of 2 which divides 2n." It is more visually seen as the height of consecutive marks on an inch ruler. This determines the increases in values between numbers in a row. The rest of the code should be straightforward.

If there are slots consisting of n numbers, which will x fit into?

This is a little bit hard to phrase... Basically, my question is as follows. If all the numbers up to z are divided into groups of n, which will x fit into. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|_______| |_______| |___________|
0 1 2
so in this example, n = 5, there are groups of five. z = 15. Let's say x = 9. How can I write a function that will return 1 for x = 9 because its in the group with the index position of 1, and that will return 2 for x = 12? I'm having trouble researching this, because I'm having a hard time explaining it. I get the feeling it's deceptively simple... I'm using python 2.7. Thanks in advance.
P.S. I hope this question follows the guidelines for good questions, I seem to be having trouble with that :P
Just divide x by n. The only hitch is that you need to subtract 1 first, because your sequence starts at 1 rather than 0.
def whichSlot(x, n):
return (x - 1) // n
If n are all the same size, it's simple integer division
(x-1)//5

Categories

Resources