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
4.1k views
in Technique[技术] by (71.8m points)

python - Conditional Row shift in Pandas

I'm attempting to shift a row based on whether or not another column is not null. There's inconsistent spacing in the Description column so I can't do a .shift()

Here's the original data

Permit Number    A      Description
1234            NaN    NaN
NaN             NaN    NaN
NaN             NaN    foo
3456            NaN    NaN
NaN             NaN    bar

And this is what I want my result to be

Permit Number    A      Description
1234            NaN    foo
NaN             NaN    NaN
NaN             NaN    NaN
3456            NaN    bar
NaN             NaN    NaN

Here's the code that I used from Align data in one column with another row, based on the last time some condition was true

mask = df['Description'].notnull()
fmask = (df['Permit Number'].notnull() & df['Description'].isnull())
df.assign(Description=df.groupby(mask[::-1].cumsum())['Description'].transform(lambda x: x.iloc[-1]).where(fmask))

However when I run it, no errors and no changes in the dataframe.


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

1 Reply

0 votes
by (71.8m points)

If you do not need the NaN rows, you can go like following. I can't test it, but let me know if there's any typo.

this is the original.

Permit Number    A      Description
1234            NaN    NaN
NaN             NaN    NaN
NaN             NaN    foo
3456            NaN    NaN
NaN             NaN    bar

I try to achieve this one first :

df['Permit Number'] = df['Permit Number'].ffill()
Permit Number    A      Description
1234            NaN    NaN
1234            NaN    NaN
1234            NaN    foo
3456            NaN    NaN
3456            NaN    bar

then

df.groupby(['Permit Number','A'])['Description'].max().reset_index()
Permit Number    A      Description
1234            NaN    foo
3456            NaN    bar

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

...