我有一个DataFrame和Series条件,如下所示:
import pandas as pd
import numpy as np
dates = pd.date_range('20170801','20170805')
df = pd.DataFrame({'Sales:[5,10,15,20,25]'}, index = dates)
error_days = pd.Series([True, True,False, False, True], index=df.index)
所以,df是
Sales
2017-08-01 5
2017-08-02 10
2017-08-03 15
2017-08-04 20
2017-08-05 25
error_days是
2017-08-01 True
2017-08-02 True
2017-08-03 False
2017-08-04 False
2017-08-05 True
如果原始销售额> 8,我会将那些error_days的销售额更改为8,因此df结果为
Sales
2017-08-01 5
2017-08-02 8
2017-08-03 15
2017-08-04 20
2017-08-05 8
怎么做?
我知道
df['sales'][error_days] = 8
会更改所有值,但我只想将这些销售额> 8更改为8。
BTW,请不要在df。
中添加新列感谢。
答案 0 :(得分:2)
首先需要&
的链式条件和另一个掩码df['Sales'] > 8
。
<强> 1 强>
使用loc
:
df.loc[error_days & (df['Sales'] > 8), 'Sales'] = 8
print (df)
Sales
2017-08-01 5
2017-08-02 8
2017-08-03 15
2017-08-04 20
2017-08-05 8
<强> 2 强>
或mask
:
df['Sales'] = df['Sales'].mask(error_days & (df['Sales'] > 8), 8)
print (df)
Sales
2017-08-01 5
2017-08-02 8
2017-08-03 15
2017-08-04 20
2017-08-05 8
第3 强>
df['Sales'] = np.where(error_days & (df['Sales'] > 8), 8, df['Sales'])
print (df)
Sales
2017-08-01 5
2017-08-02 8
2017-08-03 15
2017-08-04 20
2017-08-05 8
答案 1 :(得分:2)
IIUC我们可以在这里使用Series.clip_upper():
export const thumbnails = {
'cat1': require('./images/cat1.png'),
'cat2': require('./images/cat2.png'),
'cat3': require('./images/cat3.png'),
'cat4': require('./images/cat4.png'),
'cat5': require('./images/cat5.png'),
'cat6': require('./images/cat6.png'),
'cat7': require('./images/cat7.png'),
'cat8': require('./images/cat8.png'),
'cat9': require('./images/cat9.png'),
'cat10': require('./images/cat10.png'),
'cat11': require('./images/cat11.png'),
'cat12': require('./images/cat12.png'),
};
export default class App extends React.Component {
// For List View
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(['cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9', 'cat10', 'cat11', 'cat12', ]),
};
}
render() {
return (
<View style={{flex: 1}}>
<ScrollableTabView
renderTabBar={() => <CustomTabBar />}>
<ListView tabLabel="tab1" contentContainerStyle={{flexDirection: 'row', flexWrap: 'wrap', alignItems: 'flex-start',}}
style={{flex:1}} dataSource={this.state.dataSource} renderRow={(rowData) => <Image resizeMode="contain" style={{width: width / 3, height: width / 3}} source={thumbnails[rowData]} />} />
<View tabLabel="tab2" style={{alignItems: 'center', justifyContent: 'center', height: 300}}>
<Text style={{color: '#000'}}>Page 2 here</Text>
</View>
<View tabLabel="tab3" style={{alignItems: 'center', justifyContent: 'center', height: 300}}>
<Text style={{color: '#000'}}>Page 3 here</Text>
</View>
<View tabLabel="tab4" style={{alignItems: 'center', justifyContent: 'center', height: 300}}>
<Text style={{color: '#000'}}>Page 4 here</Text>
</View>
</ScrollableTabView>
</View>
);
}
}