1
        000012  000013   000014   ...    004004  005585  007682
0          0     3.8      3.7   ...       1.1     4.8     0.4
1          0       0      0.0   ...       0.0       5     7.8
2          0       0      0.0   ...       0.0     1.6     2.1
3          0       0      2.0   ...       2.3       0     0.4
4          0       0      1.3   ...       0.2     1.3     0.1
5          0       0      0.0   ...       0.0     4.1     3.5
6          0       0      0.0   ...       0.6     0.2     0.3
7          0       0      0.0   ...       0.0       0     7.1
8          0       0      0.0   ...       0.0       0     0.0

I have something like this. I need compare each column value to know how many times appears values greater than 1 in each column.

I have tried this:

s.set_index(s.index).gt(1).sum(1).reset_index(name='result').fillna(s)

but it gets an error: Could not operate 1 with block values '>' not supported between instances of 'numpy.ndarray' and 'int'

The values of the columns are floats.

Someone knows like I solve it?? Thanks!

2
  • In other hand, if I change gt(1.) by eq(1.) it runs. I am not have idea why Commented Dec 14, 2018 at 12:19
  • 1
    please read this before posting; How to Ask, minimal reproducible example. desired output would be helpful Commented Dec 14, 2018 at 12:23

3 Answers 3

1

i can't give you the exact code as your table isn't clear but you can try using query():-

df_filtered = df.query('a > 1')

where a is the Header of the column you are trying to filter.

to add multiple conditions you can use & in between each column

df_filtered = df.query('a > 1 & b > 1')
Sign up to request clarification or add additional context in comments.

6 Comments

The table is a csv file, there are weather stations (columns) and the data are precipitations by day. I need know, for each column, how many days the precipitation is greather than 1.
I need for all columns, and there are 86. Do you know some system to do it in all columns?
I've just updated my answer , i think you can add the condition for the 86 column and it should work
@guilleeh if you found my answer helpful please mark my answer or rate it to help others who are struggling with similar problem . thank you.
Thanks for all. I have two problems. First, my column labels are numbers and its a problem, i suppose i can change it. Second, I would have it 86 times, and it's very little eficient. Another method?
|
0

please try this code:

import pandas as pd
import numpy as np
datan = np.random.randn(36).reshape(9, 4)
df = pd.DataFrame(data=datan, columns=list("ABCD"))
output = {}
for c in df.columns:
output[c] = df[c][df[c] >= 1].sum()
df2 = pd.DataFrame(output, index=[0])
df2

Comments

0

Try this :

import pandas as pd
dc={}  #The keys will identify the column name and its value differentiate how many times appears values greater than 1 .

    for i in list(dataframe.columns.values):
      dc[i] =  dataframe.loc[dataframe[i].gt(1),i].count()  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.