Is there a way in Python DataFrame to store a specific value position (row or column) and a specific number of values in each variable?

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

Hello, I'm Python beginner who is learning Python dataframe.

fori in range(len(df))
    if df1.loc[i, "idx1"]=="HELLO" :
        cnt1=cnt1+1
        num=i
        df2.loc[10, 'idx8'] = df.at[i,'IDX8']

There's a code like this, but when I turned it into a for statement, it took longer than I thought to "execute", so I didn't use a for statement, so I had a hard time trying it.

Is there a way to get the num and cnt1 values without using for statement?

python dataframe for if문 boolean

2022-09-20 15:08

1 Answers

Is this what you want?

Pick only those with a specific column value of "HELLO" and find out the number and location.

>>> import pandas as pd
>>> df = pd.DataFrame({"idx1":["AELLO", "HELLO", "KELLO", "HELLO", "MELLO"]})
>>> df
    idx1
0  AELLO
1  HELLO
2  KELLO
3  HELLO
4  MELLO
>>> df[df.idx1 == "HELLO"]
    idx1
1  HELLO
3  HELLO
>>> len(df[df.idx1 == "HELLO"])
2
>>> df[df.idx1 == "HELLO"].index
Int64Index([1, 3], dtype='int64')


2022-09-20 15:08

If you have any answers or tips


© 2022 pinfo. All rights reserved.