Make a new file from the output obtained from for loop - python-3.x

How do we do to make the data structure of multiple (122) rows and (1) columns data into (122) rows and H_S1 H 0.16 2593.354 i.e (4) columns as a new file having extention (.txt or .csv or .dat)? Thanks in advance.
This is the structure of the "print(y,y1,y2,y3)" output obtained using for loop statement in python3.
[122 rows x 1 columns]
H_S1 H 0.16 2593.354
H_S2 H 0.32 2676.584
H_S3 H 0.64 5125.25
H_S4 H 1.28 12029.221
H_S5 H 2.56 6764.678
unc_H_S1 H 0.16 8.16627
unc_H_S2 H 0.32 10.754601
unc_H_S3 H 0.64 5.16457
unc_H_S4 H 1.28 10.93159
.
.
.
Desired output:
[122 rows x 4 columns]
H_S1 H 0.16 2593.354
H_S2 H 0.32 2676.584
H_S3 H 0.64 5125.25
H_S4 H 1.28 12029.221
H_S5 H 2.56 6764.678
unc_H_S1 H 0.16 8.16627
unc_H_S2 H 0.32 10.754601
unc_H_S3 H 0.64 5.16457
unc_H_S4 H 1.28 10.93159
.
.
.

Related

Count unique values in a MS Excel column based on values of other column

I am trying to find the unique number of Customers, O (Orders), Q (Quotations) and D (Drafts) our team has dealt with on a particular day from this sample dataset. Please note that there are repeated "Quote/Order #"s in the dataset. I need to figure out the unique numbers of Q/O/D on a given day.
I have figured out all the values except the fields highlighted in light orange color of my Expected output table. Can someone help me figure out the MS Excel formula for these four values as requested above?
Below is the given dataset. Please note that there can be empty values against a date. But those will always be found in the bottom few rows of the table:
Date
Job #
Job Type
Quote/Ordr #
Parts
Customer
man-hr
4-Apr-22
1
O
307585
1
FRU
0.35
4-Apr-22
2
D
307267
28
ATM
4.00
4-Apr-22
2
D
307267
25
ATM
3.75
4-Apr-22
2
D
307267
6
ATM
0.17
4-Apr-22
3
D
307438
3
ELCTRC
0.45
4-Apr-22
4
D
307515
7
ATM
0.60
4-Apr-22
4
D
307515
5
ATM
0.55
4-Apr-22
4
D
307515
4
ATM
0.35
4-Apr-22
5
O
307587
4
PULSE
0.30
4-Apr-22
6
O
307588
3
PULSE
0.40
5-Apr-22
1
O
307623
1
WST
0.45
5-Apr-22
2
O
307629
4
CG
0.50
5-Apr-22
3
O
307630
10
SUPER
1.50
5-Apr-22
4
O
307631
3
SUPER
0.60
5-Apr-22
5
O
307640
7
CAM
0.40
5-Apr-22
6
Q
307527
6
WG
0.55
5-Apr-22
6
Q
307527
3
WG
0.30
5-Apr-22
To figure out the unique "Number of Jobs" on Apr 4, I used the Excel formula:
=MAXIFS($K$3:$K$20,$J$3:$J$20,R3) Where, R3 ='4-Apr-22'
To figure out the unique "Number of D (Draft) Jobs" I used the Excel formula:
=SUMIFS($P$3:$P$20,$J$3:$J$20,R3,$L$3:$L$20,"D")
[1
[2

Python LIfe Expectancy

Trying to use panda to calculate life expectanc with complex equations.
Multiply or divide column by column is not difficult to do.
My data is
A b
1 0.99 1000
2 0.95 =0.99*1000=990
3 0.93 = 0.95*990
Field A is populated and field be has only the 1000
Field b (b2) = A1*b1
Tried shift function, got result for b2 only and the rest zeros any help please thanks mazin
IIUC, if you're starting with:
>>> df
A b
0 0.99 1000.0
1 0.95 NaN
2 0.93 NaN
Then you can do:
df.loc[df.b.isnull(),'b'] = (df.A.cumprod()*1000).shift()
>>> df
A b
0 0.99 1000.0
1 0.95 990.0
2 0.93 940.5
Or more generally:
df['b'] = (df.A.cumprod()*df.b.iloc[0]).shift().fillna(df.b.iloc[0])

Apply Python lambda : if condition giving syntax error

This is my data set
fake_abalone2
Sex Length Diameter Height Whole Shucked Viscera Shell Rings
Weight Weight Weight Weight
0 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.1500 15
1 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.0700 7
2 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.2100 9
3 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.1550 10
4 K 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.0550 7
5 K 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.1200 8
Getting syntax error while using the following method. Please help me out.
I want the value in "sex" table to change depending on "Rings" table.If "Rings" value is less than 10 the corresponding "sex" value should be changed to 'K'.Otherwise, no change should be made in "Sex" table.
fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)
File "", line 1
fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)
SyntaxError: invalid syntax
The Following method works perfectly.
df1["Sex"]=df1.apply(lambda x: "K"if x.Rings<10 else x["Sex"],axis=1)
df1 is the dataframe
Sex Length Diameter Height Whole Shucked Viscera Shell Rings
weight weight weight weight
0 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.1500 15
1 K 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.0700 7
2 K 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.2100 9
3 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.1550 10
4 K 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.0550 7
5 K 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.1200 8
6 F 0.530 0.415 0.150 0.7775 0.2370 0.1415 0.3300 20
You can use Python numpy instead of lambda function.
Import python numpy using import numpy as np
then you can use the following method to replace the string.
fake_abalone2['Sex'] = np.where(fake_abalone2['Rings']<10, 'K', fake_abalone2['Sex'])
The main problem is the output of the lambda function:
.apply(lambda x:"K" if x<10)
The output is not certain for other conditions, so you can use else something ...
.apply(lambda x:"K" if x<10 else None)

How to exclude 0 from MIN formula unless that is the only option?

I want excel to select the lowest number excluding 0 from the D and G Columns and display it in the H Column. Right now my table looks like this:
A B C D E F G H
1 1 150.00 52 7800.00 0 569.99 0.00 =SMALL((G5,J5),INDEX(FREQUENCY((D1,G1),0),1)+1)
2 0 50.00 52 0.00 0 750.00 0.00 =SMALL((G5,J5),INDEX(FREQUENCY((D2,G2),0),1)+1)
Row 1 works as expected and selects 7800.00 and ignores the 0, but for row 2 selects neither because they're both 0 and displays #NUM!. Any chance that I can fix this?
As Mr SeanC said it was as easy as just using =IFERROR:
=IFERROR(SMALL((G5,J5),INDEX(FREQUENCY((D2,G2),0),1)+1),0)

How to count number of peaks in graph ? -graph analysis-

I have this curve that contains certain peaks - I want to know how to get the number of these peaks.
Sample Data:
0.10 76792
0.15 35578
0.20 44675
0.25 52723
0.30 27099
0.35 113931
0.40 111043
0.45 34312
0.50 101947
0.55 100824
0.60 20546
0.65 114430
0.70 113764
0.75 15713
0.80 83133
0.85 79754
0.90 17420
0.95 121094
1.00 117346
1.05 22841
1.10 95095
1.15 94999
1.20 18986
1.25 111226
1.30 106640
1.35 34781
1.40 66356
1.45 68706
1.50 21247
1.55 117604
1.60 114268
1.65 26292
1.70 88486
1.75 89841
1.80 49863
1.85 111938
The 1st column is the X values, the 2nd column is the y values.
I want to write a macro or formula that tell me how many peaks in this graph.
Note: this graph is actualy ploted and exported from matlab, so if there is a way i can tell my code to do it for me from matlab it would be also great!
if your data was in A1:B36 then this formula
=SUMPRODUCT(--(B2:B35>B1:B34),--(B2:B35>B3:B36))
returns 11 peaks
It checks if
B2 is higher than B1 and B3, if so counts it as a peak
then if B3 is higher than B2 and B4, if so counts it as a peak and so on
[Updated: VBA request added]
Sub GetMax()
Dim chr As ChartObject
Dim chrSeries As Series
Dim lngrow As Long
On Error Resume Next
Set chr = ActiveSheet.ChartObjects(1)
Set chrSeries = chr.Chart.SeriesCollection(1)
On Error GoTo 0
If chrSeries Is Nothing Then Exit Sub
For lngrow = 2 To UBound(chrSeries.Values) - 1
If chrSeries.Values(lngrow) > chrSeries.Values(lngrow - 1) Then
If chrSeries.Values(lngrow) > chrSeries.Values(lngrow + 1) Then
chrSeries.Points(lngrow).ApplyDataLabels
With chrSeries.Points(lngrow).DataLabel
.Position = xlLabelPositionCenter
.Border.Color = 1
End With
End If
End If
Next
End Sub

Resources