Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

python - compare value with panda groupby, multiple rows

pic1

I have a dataframe that have an identical value in the column of "code" for every two rows.

I want to create a column name ['test'] on the far right side that compares the value of "V" from every groupby ['code'] and return true or false in the column of ['test']

for example,

I want to compare the value in "V"(30367315) in the first "code" of 0050 to the vale of "V"(14029981) with the second value of "0050"

0050 if 30367315 > 14029981, ['test'] return true.

0051 if 56966 > 46687, ['test'] return true.

0052 if 2447344 > 2798834, ['test'] return false.

here is what've tried:

container=[]
for _df in td2.groupby(['code']):
    _df = pd.DataFrame(_df)
    num1 = _df.iloc[1,0]
    num2 = _df.iloc[1,1]
    _df['test'] = num2 > num1
    container.append(_df)
    ch_ = pd.concat(container)

this is expected result: pic2

Today, my result returned as

IndexError: single positional indexer is out-of-bounds

but the other day I tried this code, it worked. I am not sure what I did. My jupyter notebook autosaved, and I must have touch the code somewhere and I can't figure it out why.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...