从基于其他列的列中获取值

时间:2020-03-05 10:51:02

标签: python

我想从同一df的另一列中提取具有条件的数据帧的值。

    A    B
0   2    10
1   3    60
2   7    50
3   1    100

我只想从A中获取B值大于2的值,并用固定值= 5填充从B中获取的值

赞:

    A    B
0   2    5
1   3    60
2   7    50
3   1    5

2 个答案:

答案 0 :(得分:1)

使用:

import numpy as np
df['B'] = np.where(df['A']>2,df['B'],5)
df
   A   B
0  2   5
1  3  60
2  7  50
3  1   5

答案 1 :(得分:1)

使用class AceEditor extends React.Component { constructor(props, context) { super(props, context); this.editor = React.createRef(); } componentDidMount() { this.setState({ code: "Test text" }) } render() { const {code} = this.state; return ( <div> <Button onClick={() => { this.editor.current.editor.undo() }}/> <AceEditor ref={this.editor} value={code} // more props onLoad={editor => { const session = editor.getSession(); const undoManager = session.getUndoManager(); undoManager.reset(); session.setUndoManager(undoManager); }} /> </div> ); } }

pandas.Series.where

输出:

df["B"] = df["B"].where(df["A"].gt(2), 5)
print(df)