如何知道数据框熊猫python行中文本的出现

时间:2018-08-07 09:29:42

标签: python pandas dataframe row

     C1    
0   John   
1   John  
2   John 
3   Michale
4   Michale
5   Newton
6   Newton 
7   John
8   John
9   John

我想知道约翰有多少次出现在行上。假设约翰从0到2发生,结果我希望约翰从0到2。从3到4米歇尔从5到6牛顿

我想要这种格式的结果:

Start  End   Name   
0      2     John  
3      4     Michale
5      6     newton
7      9     John

2 个答案:

答案 0 :(得分:2)

使用

@ControllerAdvice
public class WebDataBindHandler {

    @Inject
    private LocalValidatorFactoryBean localValidatorFactoryBean;

    @InitBinder
    void initBinder(WebDataBinder binder) {
        binder.addValidators(new JSR303CollectionListValidator(localValidatorFactoryBean));
    }
}

答案 1 :(得分:1)

@Zero:请将以下内容添加到您的代码帮助中吗? :)

df_new = df.reset_index().groupby('C1')['index'].agg(['min', 'max']).rename(
        columns={'min': 'start', 'max': 'end'})

df_new.reset_index().rename(columns={'C1':'Name'})

编辑:也许是这样的..?我仍在学习,但尝试没有危害。 :)

labels = (df.C1 != df.C1.shift()).cumsum()
df1 = pd.concat([df,labels],axis = 1,names = 'label')
df1.columns = ['C1','label']
df_new = df1.reset_index().groupby(['label','C1']).agg(['min', 'max']).rename(
    columns={'min': 'start', 'max': 'end'}).reset_index().rename(columns={'C1':'Name'})
df_new