I have a pivot table result as below :
len
MERCHANT_NAME
MCC_CODE
0.0 58635982
742.0 7378
763.0 750
780.0 281
1520.0 974
frame_mcc_merchant_pvt=pd.pivot_table(frame_mcc_merchant,index=['MCC_CODE'],
values=['MERCHANT_NAME'],aggfunc=[len],fill_value=0)
I need to sort the pivot table by the values of the len of merchant name ?
Pls help
Remove [] for avoid MultiIndex and then sorting by sort_values:
frame_mcc_merchant_pvt=pd.pivot_table(frame_mcc_merchant,
index='MCC_CODE',
values='MERCHANT_NAME',
aggfunc=len,
fill_value=0).sort_values('MERCHANT_NAME')
my code here is copying over the pivot table results, and then checking for the average (arithmetic mean value) and the standard deviation of a batch.
At the section where i copied over the numbers, i am sure you could sort them for size, by getting max / min and then loop through.
Private Sub EvaluateButton_Click()
Dim EvaRange As Range 'Evaluation range
Dim i As Integer 'Counter for free columns
Dim n As Integer 'Counter for number of measurements
Dim a As Integer 'Counter for Abfragenummer
Dim AbfrageZahl As Variant 'Frage nach der Abfrage
Dim z As Integer 'counter für Abfrage
Worksheets("testsheet").Activate
'MsgBox "The name of the active sheet is " & ActiveSheet.Name
z = 0
AbfrageZahl = InputBox("Bitte geben Sie eine Zahl >1 ein für die Abfrage. Q2 2022: 2, Q4 2022: 3, Q2 2023: 3 usw.")
z = AbfrageZahl - 2
Set EvaRange = Range("C10:C999")
EvaRange.Copy Cells(15, 7 + 2 * z)
Range(Cells(15, 7 + 2 * z), (Cells(Rows.Count, 7 + 2 * z))).Select
i = 15
For Each self In Selection.SpecialCells(xlCellTypeConstants)
self.Copy Cells(i, 8 + 2 * z)
i = i + 1
Next
Cells(i - 1, 8 + 2 * z).Clear 'und jetzt die Auswertung
n = i - 16 'definiere mein n
Cells(1, 8 + 2 * z).Value = Date 'schreibe Abfragedatum
Cells(2, 8 + 2 * z).Value = n 'schreibe mein n
Cells(3, 8 + 2 * z).Value = Application.WorksheetFunction.Average(Range(Cells(15, 8 + 2 * z), Cells(Rows.Count, 8 + 2 * z)))
Cells(4, 8 + 2 * z).Value = Application.WorksheetFunction.StDev_S(Range(Cells(15, 8 + 2 * z), Cells(Rows.Count, 8 + 2 * z)))
Cells(5, 8 + 2 * z).Value = Range("C1").Value
Cells(6, 8 + 2 * z).Value = Range("C2").Value
Cells(7, 8 + 2 * z).Value = Range("C3").Value
Cells(8, 8 + 2 * z).Value = Range("C4").Value
Cells(9, 8 + 2 * z).Value = Range("C5").Value
Cells(10, 8 + 2 * z).Value = Range("C6").Value
Cells(11, 8 + 2 * z).Value = Tabelle2.Name
Cells(12, 8 + 2 * z).Value = AbfrageZahl
Cells(1, 7 + 2 * z).Value = "Date of Review"
Cells(2, 7 + 2 * z).Value = "n, Number of Measurements"
Cells(3, 7 + 2 * z).Value = "µ, arithmetic mean"
Cells(4, 7 + 2 * z).Value = "Sigma, stand. Dev. of a batch"
Cells(5, 7 + 2 * z).Value = Range("B1").Value
Cells(6, 7 + 2 * z).Value = Range("B2").Value
Cells(7, 7 + 2 * z).Value = Range("B3").Value
Cells(8, 7 + 2 * z).Value = Range("B4").Value
Cells(9, 7 + 2 * z).Value = Range("B5").Value
Cells(10, 7 + 2 * z).Value = Range("B6").Value
Cells(11, 7 + 2 * z).Value = "Test Name"
Cells(12, 7 + 2 * z).Value = "Review Cycle Nr."
End Sub
Related
Examples:
m n: 2 3
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
m n: 4 2
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
I have written this code but i said "list assignment index out of range", how can I fix it? thanks
m, n = input('m n: ').split()
x = []
for i in range(0, int(m)):
for j in range(0, int(n)):
x[j] = int(m[i]) * int(n[j])
print(str(i) + ' * ' + str(j) + ' = ',x[j])
m, n = input('m n: ').split()
for i in range(1, int(m)+1):
for j in range(1, int(n)+1):
print(str(i) + ' * ' + str(j) + ' = ', i * j)
I have tried below code to return a multiplication table for input number:
def table_of(n):
for i in range(1,11):
print(n,"*",i,"=",n*i)
a = input("Enter a Number:")
table_of(a)
This returns:
Enter a Number:2
2 * 1 = 2
2 * 2 = 22
2 * 3 = 222
2 * 4 = 2222
2 * 5 = 22222
2 * 6 = 222222
2 * 7 = 2222222
2 * 8 = 22222222
2 * 9 = 222222222
2 * 10 = 2222222222
What is the problem?
The input() function returns a string therefor the loop prints that string i times. The solution would be to replace a = input(“Enter a number: ”) with
a = int(input(“Enter a number: ”))
The output from an input statement is always a string. You need to convert n to an integer before multiplying, either at the input statement, or in the print, as below.
def table_of(n):
for i in range(1,11):
print(n,"*",i,"=",int(n)*i)
a = input("Enter a Number:")
table_of(a)
Gives the output:
Enter a Number:>? 2
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
I want to print this pattern in python:(10 rows rhombus increment-decrement numbers)
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
12321
121
1
Note : 1234321 is missing in the last but fourth line, so I think it is not a perfect rhombus. Plus instead of 10, 1 is printed in the tenth line.How to achieve this shape? Please let me know.
I had no idea on how to do this using numbers so,tried the below basic program using * but I don't know how to print the same using numbers as shown above.
n=11
for i in range(n):
print(''*(n-i-1)+'* '*(i+1))
for j in range(n-1,0,-1):
print(''*(n-j)+'* '*(j))
Also this prints half the shape only
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
How to print the pattern I mentioned(using increment-decrement numbers)?
Plus is the question wrong because it misses 1234321 line or it can be achieved?
Please help me.I am new to coding so how to achieve this using the simplest way i.e using looping constructs like for loop? Thanks in advance for your help and time.
Here is a possibility for how to generate the geometry:
n = 11
for i in range(n // 2 + 1):
print(' ' * (n // 2 - i) + '* ' * (i + 1))
for i in reversed(range(n // 2)):
print(' ' * (n // 2 - i) + '* ' * (i + 1))
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
EDIT: Here is an actual implementation with numbers, that fails at n = 10, unfortunately.
from itertools import chain
n = 9
ranges = chain(range(n), reversed(range(n - 1)))
for i in ranges:
print(' ' * (n - i - 1), *[x + 1 for x in range(i + 1)],
*[x + 1 for x in reversed(range(i))])
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
I'd do a function for one line, and run it twice, skipping the 4 at the end:
def oneline(i):
for j in range(i,10):
print(' ', end='')
for j in range(1, i+1):
print(j if j<10 else 1, end='')
for j in range(i-1, 0, -1):
print(j if j<10 else 1, end='')
print()
for i in range(1,11):
oneline(i)
for i in range(9,0,-1):
if i == 4:
continue
oneline(i)
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
12321
121
1
I believe this would be more pythonic:
size = 10
pattern = (size-1)*" " + size*"123456789" # indented pattern
rhombus = [ pattern[i:i+size] for i in range(size) ] # top-left
rhombus = [ d+d[-2::-1] for d in rhombus ] # horizontal mirror
rhombus = rhombus+rhombus[-2::-1] # vertical mirror
The approach leverages the horizontal and vertical symmetry of the output by only generating the top-left corner and then mirroring horizontally and vertically
output:
for line in rhombus: print(line)
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Alternatively, if you want to print as you go, you can work with the left side of the pattern for all the lines and print the horizontally mirrored substring of the pattern for each line:
size = 10
pattern = size*" " + size*"123456789" # indented pattern (1 based)
for i in range(1,size*2): # for each line (1 based)
line = pattern[size-abs(size-i):][:size] # left side of line
print(line + line[-2::-1]) # print mirrored line
Here's a simple version using for loops. Should work for large numbers.
def r(upper=10):
for i in range(-1 * (upper-1), upper):
j = upper - abs(i)
print(' ' * (abs(i)), end ='')
for k in range(-1 * (j-1), j):
print(str(j - abs(k))[0], end='')
print()
r(10)
..
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Hope it helps :)
So I'm given the following diagram:
And I'm being asked to find the area of each polygon given n. The area is basically the sum of all blue squares since each square has an area of 1. So when n = 1, the area is one. When n = 2, the area is 5. Because of the relationship between each polygon, I know that I could knock this down using set theory.
n Area(n)
1 1
2 A(n-1) + (4 * (n-1)) = 5
3 A(n-1) + (4 * (n-1)) = 13
4 A(n-1) + (4 * (n-1)) = 25
5 A(n-1) + (4 * (n-1)) = 41
However, I didn't have as much luck trying to represent this in code:
def shapeArea(n):
prev_output = 0
output = 0
if n == 1:
output = 1
elif n > 1:
for i in range(n):
prev_output = n-1 + (4 * (n-1))
output = prev_output + (4 * (n-1))
return output
For example: For n = 2, I'm getting an output of 9 instead of 5.
You were close :-)
Here are the small fix-ups:
def shapeArea(n):
output = 1
for i in range(1, n):
output += 4 * i
return output
Running this:
for n in range(1, 6):
print(n, shapeArea(n))
Gives this output:
1 1
2 5
3 13
4 25
5 41
of course with Gauss's theorem the code would look like this:
def ShapeArea(n):
return 2*(n**2) - (2*n)+1
In a recursive solution, it is much simple from your logic.
def shape_area(n):
if n == 1:
return 1
return shape_area(n-1) + 4*(n-1)
You can use Gauss' trick to make a closed-form formula:
2*Area(n) =
1 + 4 + 8 + 12 + ... + 4(n-1)
+
1 + 4(n-1) + 4(n-2) + 4(n-3) + ... + 4
--------------------------------------------------
2 + 4n + 4n + 4n + ... + 4n
= (n-1)4n + 2
= 4n^2 - 4n + 2
So Area(n) = 2n2 - 2n + 1
def shape_area(n):
return sum([1] + [4*i for i in range(1, n)])
4 one digit numbers and try to get all the possible combos
4511
like 4 + 5 + 1 x 1
Code to get first, 2nd 3rd and 4th numbers
numbers = input("Input 4 numbers separated with , : ")
numlist = numbers.split(",")
print (numlist)
No1 = int(numlist.pop(0))
No2 = int(numlist[0])
No3 = int(numlist[1])
No4 = int(numlist[2])
An exhaustive search:
from random import randrange
from itertools import permutations
def solve(digits):
for xs in permutations(digits):
for ops in permutations('+-*/', 3):
equation = reduce(lambda r, (x, op): '{0} {1} {2}'.format(r, op, float(x)), zip(xs[1:], ops), xs[0])
try:
if eval(equation) == 10:
yield equation.replace('.0','')
except ZeroDivisionError:
pass
digits = [randrange(0,10,1) for x in range(4)]
solutions = list(solve(digits))
if solutions:
print '\n'.join(solutions)
else:
print 'No solution for {0}'.format(digits)
Example output:
2 * 5 / 1 + 0
2 * 5 / 1 - 0
2 * 5 + 0 / 1
2 * 5 - 0 / 1
2 / 1 * 5 + 0
2 / 1 * 5 - 0
5 * 2 / 1 + 0
5 * 2 / 1 - 0
5 * 2 + 0 / 1
5 * 2 - 0 / 1
5 / 1 * 2 + 0
5 / 1 * 2 - 0
0 + 2 * 5 / 1
0 + 2 / 1 * 5
0 + 5 * 2 / 1
0 + 5 / 1 * 2
0 / 1 + 2 * 5
0 / 1 + 5 * 2
or
No solution for [7, 1, 0, 2]
Note: I like the recursive expression generator referred to in the comment above, but I just don't think recursively straight off.
Use a recursive approach:
numbers = input("Input 4 numbers separated with , : ")
numlist = list(numbers)
def f(lst,carry,result):
x = lst.pop(0)
if lst == []:
return carry+x == result or \
carry-x == result or \
carry*x == result or \
carry/x == result
elif carry==None:
carry = x
return f(lst,carry,result)
else:
return f(lst,carry+x,result) or\
f(lst,carry-x,result) or\
f(lst,carry*x,result) or\
f(lst,carry/x,result)
print(f(numlist, None, 10))
Your I/O is wrong (first two lines), and I don't know if this is something that you have already tried.