Python 通过startswith方法对数据帧进行切片
输入: 输出:Python 通过startswith方法对数据帧进行切片,python,pandas,Python,Pandas,输入: 输出: df = pd.DataFrame({'A':['jfgh',23,'START',34,42,56], 'B':['cvb',7,'rtwf',65,87,23]}) 我想通过startswith方法自动提取切片df[3:6],即我想选择与值34和23相关的索引 A B 0 jfgh cvb 1 23 7 2 START rtwf 3 34 65 4 42 87 5 56 23 但它
df = pd.DataFrame({'A':['jfgh',23,'START',34,42,56], 'B':['cvb',7,'rtwf',65,87,23]})
我想通过startswith方法自动提取切片df[3:6]
,即我想选择与值34和23相关的索引
A B
0 jfgh cvb
1 23 7
2 START rtwf
3 34 65
4 42 87
5 56 23
但它返回AttributeError:
“Series”对象没有属性“StartWith”
我认为通过选择df['A'],它将返回一个序列,从而启用startswith()的执行?更改此选项:
for index in range(len(df)):
if df['A'].startswith('START'):
df1 = df[index+1:len(df)]
break
为此:
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)
我注意到,如果在df中用一些随机字母替换“START”,但在startswith()中保留相同的字符串“START”,则返回相同的答案
A B
3 34 65
4 42 87
5 56 23