I think I am forgetting a simple formatting button but basically I have data like
x 1
x 2
x 3
x 4
x 5
x 6
x 7
y 8
y 9
y 10
y 11
y 12
y 13
z 14
z 15
z 16
z 17
z 18
z 19
z 20
How would I isolate the first X, Y, and Z cells, giving me the numbers 1, 8, and 14? etc.
So every new piece of text (i.e. A, b, c) it would isolate only the first row that contained the new content.
Related
Assume I have a dataset with three inputs:
x1 x2 x3
0 a b c
1 d e f
2 g h i
3 j k l
4 m n o
5 p q r
6 s t u
:
:
0,1,2,3 are times, x1, x2, x3 are inputs that are measured. So here x1 inputs are measured at every one hour. x2 and x3 will be measured at different time. What I need to do , I want write that what ever the measured in x1, x2, x3 it will add and subtract the values are equal to the x1 input next time value
So here what I want to do is:
x1 x2 x3 y
0 a b c a+b-c=d
1 d e f d+e-f=g
2 g h i g+h-i=j
3 j k l j+k-l=m
4 m n o m+n-o=p
5 p q r p+q-r=s
6 s t u s+t-u=v
:
:
Here with my actual data according to my csv file:
X1 x2 x3 y
0 63 0 0 63+0-0=63
60(min) 63 0 2 63+0-2 =104
120 104 11 0 104+11-0=93
180 93 0 50 93+0-50=177
240 177 0 2 177+0-2=133
300 133 0 0 133+0-0=next value of x1
I tried shift method and it didn't work for me what I want exactly. I tried another method and it worked, but didn't came as I want. Here I upload the code.
Code :
data = pd.read_csv('data6.csv')
i=0
j=1
while j < len(data):
j=data['x1'][i] - data['x2'][i] + data['x3'][i]
i+=1
j!=i
print(j)
This is works , but it is just showing only one data
63
In my csv file this is second input value of x1 input.
I want to write this code contonously happened and read the value as I shown above.
Can anyone help me to solve this problem?
My csv file
Try:
>>> df['y'] = df['x1'] + '+' + df['x2'] + '-' + df['x3'] + '!=' + df.shift(-1)['x1']
>>> df
x1 x2 x3 y
0 a b c a+b-c!=d
1 d e f d+e-f!=g
2 g h i g+h-i!=j
3 j k l j+k-l!=m
4 m n o m+n-o!=p
5 p q r p+q-r!=s
6 s t u NaN
>>>
I found the answer for this with your help. Thank you very much for helping me. #Adam.Er8 , #U10-Forward and #anky_91
Here is my code:
df = pd.DataFrame(data)
df['y'] = 0
for i in range(len(df)-1): #iterating between all the rows of dataframe
df['y'].iloc[i] == df['x1'].iloc[i] + df['x2'].iloc[i] - df['x3'].iloc[i]
df['y'].iloc[i] = df['x1'].iloc[i+1]
Excel formula - If A=1 B=2.......Z=26. If you input CAT in cell it should display the result 24 ie C+A+T. Not VB or JAVA or any programming language just the excel formula.
This is what I tried
=SUM(LOOKUP({"C","A","T"},B3:B28,C3:C28))
with input of below in the cells B3:B28,C3:C28. I want the result to display when I put in CAT in the cell.
A 1
B 2
C 3
D 4
E 5
F 6
G 7
H 8
I 9
J 10
K 11
L 12
M 13
N 14
O 15
P 16
Q 17
R 18
S 19
T 20
U 21
V 22
W 23
X 24
Y 25
Z 26
Use SUMPRODUCT to iterate the letters and use CODE to return the value:
=SUMPRODUCT(CODE(UPPER(MID(A1,ROW($XFD$1:INDEX($XFD:$XFD,LEN(A1))),1)))-64)
I have a dataset which has a row for each loan, and a borrower can have multiple loans. The 'Property' flag shows if there is any security behind the loan. I am trying to aggregate this flag on a borrower level, so for each borrower, if one of the Property flags is 'Y', I want to add an additional column where it is 'Y' for each of the borrowers.
The short example below shows what the end result should look like. Any help would be appreciated.
import pandas as pd
data = {'Borrower': [1,2,2,2,3,3,4,5,6,6],
'Loan' : [1,2,3,4,5,6,7,8,9,10],
'Property': ["Y","N","Y","Y","N","Y","N","Y","N","N"],
'Result': ['Y','Y','Y','Y','Y','Y','N','Y','N','N']}
df = pd.DataFrame.from_dict(data)
You can use Transform on Property after groupby Borrower. Because the ASCII code of 'Y' is bigger than 'N' so if there is any property which is 'Y' for a borrower, max(Property) will give 'Y'.
df['Result2'] = df.groupby('Borrower')['Property'].transform(max)
df
Out[202]:
Borrower Loan Property Result Result2
0 1 1 Y Y Y
1 2 2 N Y Y
2 2 3 Y Y Y
3 2 4 Y Y Y
4 3 5 N Y Y
5 3 6 Y Y Y
6 4 7 N N N
7 5 8 Y Y Y
8 6 9 N N N
9 6 10 N N N
I have a dataframe something like
A B C
1 4 x
2 8 y
3 7 z
4 12 y
5 10 b
i need to modify column B based on condition something like
if B <= 5 then B = 1
if B > 5 and B <= 10 then B = 2
if B > 10 and B < 15 then B = 3
so that my dataframe becomes
A B C
1 1 x
2 2 y
3 2 z
4 3 y
5 2 b
i am okay if I have to add a new column first and then drop column B. Could anyone help please?
You should use the apply function to implement this.
def check(row):
if (row['B']) <= 5:
return 1
elif (row['B'] > 5) and (row['B'] <= 10):
return 2
elif (row['B'] > 10) and (row['B'] <= 15):
return 3
These would apply the function to each row and then you can perform the checks.
df['B'] = df.apply(check, axis = 1)
Then the resulting DF would look like:
A B C
1 1 x
2 2 y
3 2 z
4 3 y
5 2 b
More documentation available here.
Given the following data frame and pivot table:
df=pd.DataFrame({'A':['a','a','a','a','a','b','b','b','b'],
'B':['x','y','z','x','y','z','x','y','z'],
'C':['a','b','a','b','a','b','a','b','a'],
'D':[7,5,3,4,1,6,5,3,1]})
table = pd.pivot_table(df, index=['A', 'B','C'],aggfunc='sum')
table
D
A B C
a x a 7
b 4
y a 1
b 5
z a 3
b x a 5
y b 3
z a 1
b 6
I want the pivot table exactly how it is, minus index level 0, like this:
D
B C
x a 7
b 4
y a 1
b 5
z a 3
x a 5
y b 3
z a 1
b 6
Thanks in advance!
You can selectively drop an index level using reset_index with param drop=True:
In [95]:
table.reset_index('A', drop=True)
Out[95]:
D
B C
x a 7
b 4
y a 1
b 5
z a 3
x a 5
y b 3
z a 1
b 6
You can use droplevel on index:
table.index = table.index.droplevel(0).