6

I have a DataFrame like this

                    gauge       satellite
1979-06-23 18:00:00 6.700000    2.484378
1979-06-27 03:00:00 NaN         8.891460
1979-06-27 06:00:00 1.833333    4.053460
1979-06-27 09:00:00 NaN         2.876649
1979-07-31 18:00:00 6.066667    1.438324

I want to obtain a DataFrame Like this

                    gauge       satellite
1979-06-23 18:00:00 6.700000    2.484378
1979-06-27 03:00:00 NaN         NaN
1979-06-27 06:00:00 1.833333    4.053460
1979-06-27 09:00:00 NaN         NaN
1979-07-31 18:00:00 6.066667    1.438324

5 Answers 5

7

What I will do reindex

df.dropna().reindex(df.index)
Sign up to request clarification or add additional context in comments.

2 Comments

Woooo... now, that is creative. +1
@modyrockett yw :-) happy coding
3

mask:

df.mask(df.gauge.isna())

                        gauge  satellite
1979-06-23 18:00:00  6.700000   2.484378
1979-06-27 03:00:00       NaN        NaN
1979-06-27 06:00:00  1.833333   4.053460
1979-06-27 09:00:00       NaN        NaN
1979-07-31 18:00:00  6.066667   1.438324

Comments

2

use np.where to add nan

import numpy as np
df['satellite'] = np.where(df['gauge'].isnull(),np.nan,df['satellite'])

Second solution

use .loc and isnull

df.loc[df['guage'].isnull(),'satellite'] = np.nan

Comments

2

You can use np.where:

df['satellite'] = np.where(df['gauge'].isna(), np.NaN, df['satellite'])
df['gauge'] = np.where(df['satellite'].isna(), np.NaN, df['gauge'])

Comments

2

You need to find if a row has np.nan. .any(1) gives you masking for a row.

df.loc[df.isna().any(1)] = np.nan

Output:

                        gauge       satellite
1979-06-23  18:00:00    6.700000    2.484378
1979-06-27  03:00:00    NaN         NaN
1979-06-27  06:00:00    1.833333    4.053460
1979-06-27  09:00:00    NaN         NaN
1979-07-31  18:00:00    6.066667    1.438324

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.