Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...