繁体   English   中英

通过startswith方法切片数据帧

[英]slicing a dataframe via the startswith method

输入:

df = pd.DataFrame({'A':['jfgh',23,'START',34,42,56], 'B':['cvb',7,'rtwf',65,87,23]})

输出:

       A     B
0   jfgh   cvb
1     23     7
2  START  rtwf
3     34    65
4     42    87
5     56    23

我想通过startswith方法自动提取切片df[3:6] ,即我想选择与值34和23相关的索引。

for index in range(len(df)):
    if df['A'].startswith('START'):
        df1 = df[index+1:len(df)]
        break

但它返回 AttributeError: 'Series' object has no attribute 'startswith'

我认为通过选择 df['A'] 它会返回一个系列,从而启用 startswith() 的执行?

改变这个:

if df['A'].startswith('START'):

对此:

if str(df.loc[index,'A']).startswith('START'):

完整代码:

for index in range(len(df)):
    if str(df.loc[index,'A']).startswith('START'):
        df1 = df[index+1:len(df)]
        break
print(df1)

输出

    A   B
3  34  65
4  42  87
5  56  23

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM