根据if条件和Shift函数获取以前的行-Python数据框

时间:2019-05-29 12:13:45

标签: python python-3.x pandas dataframe

我有如下数据。我想根据两个条件选择行。

1)以数字(1、2、3等)开头的行

2)满足第一条件的记录的上一行

请找到输入数据的外观

enter image description here

请找到我期望的输出结果

enter image description here

我尝试使用shift(-1)函数,但似乎抛出错误。我确定我搞砸了逻辑/语法。请在下面尝试的代码中找到

 # i get the index of all records that start with number.    
 s=df1.loc[df1['VARIABLE'].str.contains('^\d')==True].index 
 # now I need to get the previous record of each group but this is 
                                                         #incorrect
 df1.loc[((df1['VARIABLE'].shift(-1).str.contains('^\d')==False) & 
 (df1['VARIABLE'].str.contains('^\d')==True))].index

1 个答案:

答案 0 :(得分:0)

使用:

import React, { Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { createBottomTabNavigator, createAppContainer } from 'react- 
navigation';
import Icon from 'react-native-vector-icons/FontAwesome';

class HomeScreen extends Component {

    static navigationOptions = {
        title: 'Home'
    };

 render(){
     return (
         <View style={{ flex:1, alignItems:'center', justifyContent:'center' 
         }}>
             <Text>Home Screen</Text>
         </View>
     );
  }
  }

  const RootStack = createBottomTabNavigator(
      {
          Home: {
              screen: HomeScreen,
              navigationOptions: {
              tabBarLabel: 'Home',
              tabBarIcon: ({ tintColor }) => (
                  <Icon name = 'home' size={25} color={tintColor} />
              )
          }
      },
   },

   {
        tabBarOptions: {
        showIcon:true,
        tintColor:'red'
   }
  }
  );

  const AppContainer = createAppContainer(RootStack);

  const styles = StyleSheet.create({

 })

export default class App extends Component{
   render(){
   return <AppContainer />;
   }
}