'Pivot table sorting
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
Solution 1:[1]
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')
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jezrael |
Solution 2 | Hannes Ulbricht |