仅在同一列中的两个值之间填充 NA
Fill NA only between two values in the same column
我有一个这样的数据框:
VESSEL_NAME FAILURE
0 AAGI - AKAGISAN LCA
1 AAGI - AKAGISAN NaN
2 AAGI - AKAGISAN NaN
3 AAGI - AKAGISAN NaN
4 AAGI - AKAGISAN NaN
5 AAGI - AKAGISAN NaN
6 AAGI - AKAGISAN TCA
7 AAGI - SHANDONG DA REN NaN
8 AAGI - SHANDONG DA REN LCA
9 AAGI - SHANDONG DA REN NaN
10 AAGI - SHANDONG DA REN NaN
11 AAGI - SHANDONG DA REN NaN
12 AAGI - SHANDONG DA REN NaN
13 AAGI - SHANDONG DA REN TCA
14 AAGI - SEA MARANHAO LCA
15 AAGI - SEA MARANHAO NaN
16 AAGI - SEA MARANHAO NaN
17 AAGI - SEA MARANHAO NaN
18 AAGI - SEA MARANHAO TCA
仅当前一行值为“LCA”(如预期的 DF)时,我才需要“填充”列“FAILURE”:
VESSEL_NAME FAILURE
0 AAGI - AKAGISAN LCA
1 AAGI - AKAGISAN FILL
2 AAGI - AKAGISAN FILL
3 AAGI - AKAGISAN FILL
4 AAGI - AKAGISAN FILL
5 AAGI - AKAGISAN FILL
6 AAGI - AKAGISAN TCA
7 AAGI - SHANDONG DA REN NaN
8 AAGI - SHANDONG DA REN LCA
9 AAGI - SHANDONG DA REN FILL
10 AAGI - SHANDONG DA REN FILL
11 AAGI - SHANDONG DA REN FILL
12 AAGI - SHANDONG DA REN FILL
13 AAGI - SHANDONG DA REN TCA
14 AAGI - SEA MARANHAO LCA
15 AAGI - SEA MARANHAO FILL
16 AAGI - SEA MARANHAO FILL
17 AAGI - SEA MARANHAO FILL
18 AAGI - SEA MARANHAO TCA
有什么办法吗?我尝试了几次,但 none 都成功了。
怎么样:
import pandas as pd
df = pd.DataFrame({'FAILURE':['TCA', np.nan, 'LCA', np.nan, np.nan, 'TCA', np.nan, 'LCA', np.nan]})
fill_value = 'FILL'
where_value = 'LCA'
df['FAILURE'] = df['FAILURE'].fillna(fill_value).where(df['FAILURE'].fillna(method='ffill').eq(where_value) | df['FAILURE'].notna())
输入:
FAILURE
0 TCA
1 NaN
2 LCA
3 NaN
4 NaN
5 TCA
6 NaN
7 LCA
8 NaN
结果:
FAILURE
0 TCA
1 NaN
2 LCA
3 FILL
4 FILL
5 TCA
6 NaN
7 LCA
8 FILL
我有一个这样的数据框:
VESSEL_NAME FAILURE
0 AAGI - AKAGISAN LCA
1 AAGI - AKAGISAN NaN
2 AAGI - AKAGISAN NaN
3 AAGI - AKAGISAN NaN
4 AAGI - AKAGISAN NaN
5 AAGI - AKAGISAN NaN
6 AAGI - AKAGISAN TCA
7 AAGI - SHANDONG DA REN NaN
8 AAGI - SHANDONG DA REN LCA
9 AAGI - SHANDONG DA REN NaN
10 AAGI - SHANDONG DA REN NaN
11 AAGI - SHANDONG DA REN NaN
12 AAGI - SHANDONG DA REN NaN
13 AAGI - SHANDONG DA REN TCA
14 AAGI - SEA MARANHAO LCA
15 AAGI - SEA MARANHAO NaN
16 AAGI - SEA MARANHAO NaN
17 AAGI - SEA MARANHAO NaN
18 AAGI - SEA MARANHAO TCA
仅当前一行值为“LCA”(如预期的 DF)时,我才需要“填充”列“FAILURE”:
VESSEL_NAME FAILURE
0 AAGI - AKAGISAN LCA
1 AAGI - AKAGISAN FILL
2 AAGI - AKAGISAN FILL
3 AAGI - AKAGISAN FILL
4 AAGI - AKAGISAN FILL
5 AAGI - AKAGISAN FILL
6 AAGI - AKAGISAN TCA
7 AAGI - SHANDONG DA REN NaN
8 AAGI - SHANDONG DA REN LCA
9 AAGI - SHANDONG DA REN FILL
10 AAGI - SHANDONG DA REN FILL
11 AAGI - SHANDONG DA REN FILL
12 AAGI - SHANDONG DA REN FILL
13 AAGI - SHANDONG DA REN TCA
14 AAGI - SEA MARANHAO LCA
15 AAGI - SEA MARANHAO FILL
16 AAGI - SEA MARANHAO FILL
17 AAGI - SEA MARANHAO FILL
18 AAGI - SEA MARANHAO TCA
有什么办法吗?我尝试了几次,但 none 都成功了。
怎么样:
import pandas as pd
df = pd.DataFrame({'FAILURE':['TCA', np.nan, 'LCA', np.nan, np.nan, 'TCA', np.nan, 'LCA', np.nan]})
fill_value = 'FILL'
where_value = 'LCA'
df['FAILURE'] = df['FAILURE'].fillna(fill_value).where(df['FAILURE'].fillna(method='ffill').eq(where_value) | df['FAILURE'].notna())
输入:
FAILURE
0 TCA
1 NaN
2 LCA
3 NaN
4 NaN
5 TCA
6 NaN
7 LCA
8 NaN
结果:
FAILURE
0 TCA
1 NaN
2 LCA
3 FILL
4 FILL
5 TCA
6 NaN
7 LCA
8 FILL