我有一个数据框,例如:
A B C
27.00 9.90 6.24899992
18.00 6.90 4.827007354
15.00 4.20 2.252776065
7.50 2.90 1.673376053
3.00 3.50 3.233439065
4.00 1.20 4.254737365
3.00 2.30 1.257349325
0 8.90 0.254932365
1.00 0.90 2.233293435
现在如果 A 列有 0,我想将 B 列和 C 列中的其余数据从前 5 行删除到底行,如下所示:
A B C
27.00 9.90 6.24899992
18.00 6.90 4.827007354
15.00 NaN NaN
7.50 NaN NaN
3.00 NaN NaN
4.00 NaN NaN
3.00 NaN NaN
0 NaN NaN
1.00 NaN NaN
我的数据框的另一个示例:
A B C
27.00 9.90 6.24899992
18.00 6.90 4.827007354
15.00 4.20 2.252776065
7.50 2.90 1.673376053
3.00 NaN NaN
4.00 NaN NaN
3.00 NaN NaN
2.80 NaN NaN
1.00 NaN NaN
我想要的结果是相同的数据,因为它在 A 列中没有 0,如下所示:
A B C
27.00 9.90 6.24899992
18.00 6.90 4.827007354
15.00 4.20 2.252776065
7.50 2.90 1.673376053
3.00 NaN NaN
4.00 NaN NaN
3.00 NaN NaN
2.80 NaN NaN
1.00 NaN NaN
我怎样才能实现这一目标?
最佳答案
如果要在 B
和 C 列中的第一个
使用:0
之前设置 5 个值,然后在第一个 0 到 NaN
之后设置所有值
N = 5
m = df['A'] == 0
idx = next(iter(m.index[m]), df.index[-1] + 1)
print (idx)
2
#if possible less like N rows before first 0 add max
first = max(idx - N, 0)
print (first)
7
df.iloc[first:, df.columns.get_indexer(['B','C'])] = np.nan
print (df)
A B C
0 27.0 9.9 6.249000
1 18.0 6.9 4.827007
2 15.0 NaN NaN
3 7.5 NaN NaN
4 3.0 NaN NaN
5 4.0 NaN NaN
6 3.0 NaN NaN
7 0.0 NaN NaN
8 1.0 NaN NaN
如果A
列中没有值0
:
N = 5
m = df['A'] == 0
idx = next(iter(m.index[m]), df.index[-1] + 1)
print (idx)
9
#if possible less like N rows before first 0 add max
first = max(idx - N, 0)
print (first)
4
df.iloc[first:, df.columns.get_indexer(['B','C'])] = np.nan
print (df)
A B C
0 27.0 9.9 6.249000
1 18.0 6.9 4.827007
2 15.0 4.2 2.252776
3 7.5 2.9 1.673376
4 3.0 NaN NaN
5 4.0 NaN NaN
6 3.0 NaN NaN
7 10.0 NaN NaN
8 1.0 NaN NaN
第一个解决方案:
#create mask
m = df['A'] == 0
#cumulative sum of mask - return Trues for all values after first 0
m1 = m.cumsum() > 0
#counter of values above 0 with swapping order by indexing [::-1] and cumulative sum
s = m.iloc[::-1].cumsum()
#create counter and compare by 5
m2 = s.groupby(s).cumcount() < 5
#chain masks by | for bitwise OR
mask = m1 | m2.sort_index()
#set NaNs by mask
df[['B','C']] = df[['B','C']].mask(mask)
print (df)
关于pandas - 在 pandas 数据框中特定条件下删除其余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797092/