user3656142
user3656142

Reputation: 467

How to take use the RangeIndex method combined with a condition based on a column value's condition?

I want to take the first 300 rows of my csv file such that none of the rows's value for a particular column is 0. Can it be done while reading it the file, or should I modify the dataframe accordingly once I have read in the csv file?

I tried RangeIndex but couldn't figure out how to include the non-zero constraint in the index values

What I want:

row_val = 0
if [column_value at row]!=0:
    read in the row to the df
    row_val++
    if (row_val==300):
        break

Upvotes: 0

Views: 44

Answers (1)

BENY
BENY

Reputation: 323226

I will recommend do it after read whole csv file

df=pd.read_csv('yourfile.csv')
subdf=df.loc[df.col!=0].iloc[:300,:]

Upvotes: 2

Related Questions