how I can count signs columns?

user15649753

How I can count the number of repetitive positive or negative elements in each row?

Suppose I have the following data:

ski      2020    2021      2022     2023       2024      2025
book      1.2     5.6       8.4      -2         -5         6
jar       4.2      -5        -8      2          4           6
kook       -4      -5.2      -2.3    -5.6        -7        8

The output is a list for each row that counts the number of similar signs. For example in the first row we have 3 positive elements and then 2 negative and again one positive. So the output is [3,-2,1]. and for 2 other rows the output is as follows:

 jar   [1,-2,3]
 kook   [-5,1]
Shubham Sharma

Let us try:

s = np.sign(df.set_index('ski').stack())
s.groupby([pd.Grouper(level=0), s.diff().ne(0).cumsum()]).sum().groupby(level=0).agg(list)

ski
book    [3.0, -2.0, 1.0]
jar     [1.0, -2.0, 3.0]
kook         [-5.0, 1.0]
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can I split a dataframe by two columns and count number of rows based on group more efficient

How can I separate two columns of count by group by?

How can i get a count(*) of all the columns in a table? Using PostgreSql

How can I count the number of integer occurences from a txt file from certain columns in Java?

Gnuplot, how can I count rows and columns of a matrix input of my gnuplot script?

How can I count duplicate rows according to specific columns?

In R, how can I add mulitple columns with 0/1 count based on another column?

How can I get the count and newest record for each pair of columns?

How can I pad all equal signs in Python except for ==, !=, >=, <=?

Can I count multiple columns with Group By?

How can I apply a function to columns in a Pandas dataframe that includes a count of NaN in each column?

How can I count the number of columns a value has after a Where?

How can I echo dollar signs?

How can I get the count of the amount of grouped columns as a column?

How can I strip dollar signs ($) from a field in Hive?

how can I count the status

How can I count columns in phalcon volt?

How can I count the number of

How can I extract the value between 2 signs?

How can I count replicate values across columns?

How can I count unique values by columns

Can I echo two percent signs to a file?

How can I Count the number of columns in a .txt file in VBA

How can I count values that can be grouped by another columns in R

How can I group with multiple columns and count?

How can I count the nominal data from multiple columns

How can I count the amount of values in different columns in oracle plsql

How can I groupby a DataFrame at the same time I count the values and put in different columns?

how count repetitive unchanged signs of a column?