![](/img/trans.png)
[英]Multiply all columns of a multi-indexed DataFrame by appropriate values in a Series
[英]Mask dataframe with another multi-indexed Series
我有一个数据框,我想用多索引系列的布尔值掩盖(转换为NaN),其中该系列的多索引也是该数据框中的列名。 例如,如果df
为:
df = pd.DataFrame({ 'A': (188, 750, 1330, 1385, 188, 750, 810, 1330, 1385),
'B': (1, 2, 4, 5, 1, 2, 3, 4, 5),
'C': (2, 5, 7, 2, 5, 5, 3, 7, 2),
'D': ('foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar', 'bar') })
A B C D
0 188 1 2 foo
1 750 2 5 foo
2 1330 4 7 foo
3 1385 5 2 foo
4 188 1 5 bar
5 750 2 5 bar
6 810 3 3 bar
7 1330 4 7 bar
8 1385 5 2 bar
而多索引系列ser
为:
arrays = [('188', '750', '810', '1330', '1385'),
('1', '2', '3', '4', '5')]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['A', 'B'])
ser = pd.Series([False, False, True, False, True], index=index)
A B
188 1 False
750 2 False
810 3 True
1330 4 False
1385 5 True
dtype: bool
我怎么能掩盖(转换为NAN)在列的值C
在df
,其中的条目是False
的系列ser
,以结束与最后的数据帧,将是这样的:
A B C D
0 188 1 2 foo
1 750 2 5 foo
2 1330 4 7 foo
3 1385 5 NaN foo
4 188 1 5 bar
5 750 2 5 bar
6 810 3 NaN bar
7 1330 4 7 bar
8 1385 5 NaN bar
更改ser
的初始化步骤:
arrays = [('188', '750', '810', '1330', '1385'),
('1', '2', '3', '4', '5')]
# Note: The change is in this step - make the levels numeric.
tuples = list(zip(*map(pd.to_numeric, arrays)))
index = pd.MultiIndex.from_tuples(tuples, names=['A', 'B'])
ser = pd.Series([False, False, True, False, True], index=index)
初始化index
的级别,使其具有与“ A”和“ B”相同的dtype。 希望这不是问题。
这将让我们建立使用更简单的解决loc
和基于索引的选择和分配。
u = df.set_index(['A', 'B'])
u.loc[ser.index[ser], 'C'] = np.nan
u.reset_index()
A B C D
0 188 1 2.0 foo
1 750 2 5.0 foo
2 1330 4 7.0 foo
3 1385 5 NaN foo
4 188 1 5.0 bar
5 750 2 5.0 bar
6 810 3 NaN bar
7 1330 4 7.0 bar
8 1385 5 NaN bar
如果遇到给定ser
且需要更改索引pd.Index.set_levels
则可以使用pd.Index.set_levels
的列表理解来快速重建它。
ser.index = ser.index.set_levels([l.astype(int) for l in ser.index.levels])
# Alternative,
# ser.index = ser.index.set_levels([
# pd.to_numeric(l) for l in ser.index.levels])
现在,这有效:
u = df.set_index(['A', 'B'])
u.loc[ser.index[ser], 'C'] = np.nan
u.reset_index()
A B C D
0 188 1 2.0 foo
1 750 2 5.0 foo
2 1330 4 7.0 foo
3 1385 5 NaN foo
4 188 1 5.0 bar
5 750 2 5.0 bar
6 810 3 NaN bar
7 1330 4 7.0 bar
8 1385 5 NaN bar
注意loc
的ser.index[ser]
索引步骤,我们使用ser
的索引而不是直接使用index
。
采用:
# Converting ser to a dataframe
ndf = pd.DataFrame(ser).reset_index()
# Fetching B values against which C values needs to be mapped to NaN
idx = ndf[ndf.iloc[:,2] == True].B.values
# Fetching df index where C values needs to be mapped to NaN
idx_ = df[df.B.isin(idx)].index
# Mapping of C values to NaN
df.loc[idx_,'C'] = np.NaN
+---+------+---+-----+-----+
| | A | B | C | D |
+---+------+---+-----+-----+
| 0 | 188 | 1 | 2.0 | foo |
| 1 | 750 | 2 | 5.0 | foo |
| 2 | 1330 | 4 | 7.0 | foo |
| 3 | 1385 | 5 | NaN | foo |
| 4 | 188 | 1 | 5.0 | bar |
| 5 | 750 | 2 | 5.0 | bar |
| 6 | 810 | 3 | NaN | bar |
| 7 | 1330 | 4 | 7.0 | bar |
| 8 | 1385 | 5 | NaN | bar |
+---+------+---+-----+-----+
将isin
用于两个MultiIndex
之间的检查成员资格:
#convert columns to strings for same types of levels
df[['A','B']] = df[['A','B']].astype(str)
df.loc[df.set_index(['A','B']).index.isin(ser.index[ser]), 'C'] = np.nan
print (df)
A B C D
0 188 1 2.0 foo
1 750 2 5.0 foo
2 1330 4 7.0 foo
3 1385 5 NaN foo
4 188 1 5.0 bar
5 750 2 5.0 bar
6 810 3 NaN bar
7 1330 4 7.0 bar
8 1385 5 NaN bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.