Get max value in pandas dataframe [duplicate] - python-3.x

This question already has an answer here:
Determining the cumulative maximum of a column
(1 answer)
Closed 4 years ago.
I want to create a new column in a Pandas Dataframe which includes the maximum value of another column up to this Point in time.
For example:
time | value | max value
1 2 2
2 4 4
3 3 4
4 3 4
5 6 6
Does someone have an idea how to create a Code for the "max value" column?
Thanks

Use cummax:
df['max value'] = df['value'].cummax()
Output:
time value max value
0 1 2 2
1 2 4 4
2 3 3 4
3 4 3 4
4 5 6 6

What you said is the definition of cummax.
Just out of curiosity, you can also use expanding, which might come to be useful if you want to apply a custom function it.
df.value.expanding().max() # or .apply(custom_function)
0 2.0
1 4.0
2 4.0
3 4.0
4 6.0

Related

Return the max value in a new column for each repeating value in the other column [duplicate]

This question already has answers here:
How to assign a name to the size() column?
(5 answers)
Closed 11 months ago.
Background: I was hoping to generate a new column named: datasample based on another column named: end_bin from a table.
Question: Is there a way to return the max value in each row of the new column if the value is repeated in the previous column.
Expected result:
end_bin
datasample
6
1
8
1
10
1
2
3
3
1
2
3
2
3
I couldnt find a method to do this in pandas, any help is appreciated:)
Your question is unclear, but it looks like you want the size per group:
df['datasample'] = df.groupby('end_bin')['end_bin'].transform('size')
Output:
end_bin datasample
0 6 1
1 8 1
2 10 1
3 2 3
4 3 1
5 2 3
6 2 3

compare two data frames and update value in one data frame by comparing another data frame value

I have two data frames. Examples:
df1:
A B C
5 7 6
8 1 1
1 0 7
3 4 9
5 7 4
9 2 0
df2:
A B C
3 2 1
6 5 7
9 7 9
1 1 2
6 4 5
0 8 6
Both data frames have same index.
What I want is , wherever df1's value is less than 5,
I want to update df2's value to 0, else keep it same.
I tried the following code:
df2[df1<5]=0
but when I am printing df2, its showing same values as original df2.
I know I am missing something really simple.
Please help me.
Thank you.

Pandas DataFrame: how do we keep columns based on the index name?

I seem to run into some python or enumerate bugs that I am not quite sure how to fix it (See here for more details).
Long story short, I desire to see multiple data sets that has a column name of 0,4,6,8,10,12,14.
0 4 6 8 10 12
1 2 5 4 2 1
5 3 0 1 5 10
....
But my current data looks like the following
0 4 2 6 8 10 12
1 2 5 4 2 1
5 3 0 1 5 10
....
Therefore, I would like to add a code that keeps columns based on the index number (including only 0,4,6,8,10,12).
Is there a pandas function that can help with this?

how find unique value from Different column

A B ANSWER
1 1 1
3 3 3
1 2 1
2 4 2
4 4 4
5 5 5
6 6 6
i have used this function to get above answer "=IF(ISERROR(MATCH(A2:A8,$B$1:$B$8,0)),"",A2)"
but I need answer like this i have given below (suppose if you take value in A column "1"
Which is repeated only once in column B)
A B ANR
1 1 1
3 3 3
1 2 0
2 4 2
4 4 4
5 5 5
6 6 6
I've just wrapped your formula in a condition that returns 0 where the count of the A value from start to the current row is more than one:
=IF(COUNTIF(A$1:A2,A2)>1,0,IF(ISERROR(MATCH(A2:A8,$B$1:$B$8,0)),"",A2))
.
An alternative formula that gives the same results as above for the sample data provided but may (or may not) suit the additional requirements mentioned in a comment:
=IF(COUNTIF(A$2:A$10,A2)<=COUNTIF(B$2:B$10,A2),A2,IF(COUNTIF(A$2:A2,A2)>COUNTIF(B$2:B$10,A2),0,IF(COUNTIF(A$2:A$10,A2)>COUNTIF(B$2:B2,A2),A2,0)))

Data fill in specific pattern

I am trying to fill data in MS Excel. I am given following pattern:
1 2
1
1
2 5
2 5
2
3
3 6
3
4
4
5 4
And I want my output in following format:
1 2
1 2
1 2
2 5
2 5
2 5
3 6
3 6
3 6
4
4
5 4
I tried using if(b2,b2,c1) in column 3. but that doesn't solve the problem for a=3 and a=4.
Any idea how to do this in Excel?
With sorting thus:
(the effect of which in this case is merely to move 6 up once cell) and a blank row above:
=IF(AND(A2<>A1,B2=""),"",IF(B2<>"",B2,C1))
In C2 and copied down should get the result you ask for from the data sample provided.

Resources