在 pandas 数据帧中高效搜索字符串的第一个字符



我有一个熊猫数据框列,我需要修改该列中以 2 开头的任何条目。现在,我正在使用这个有效,但非常非常慢:

for i, row in df.iterrows():
    if df['IDnumber'][i].startswith('2') == True:
       '''Do some stuff'''

我觉得(阅读:知道)有一种更有效的方法可以在不使用 for 循环的情况下做到这一点,但我似乎找不到它。

我尝试过的其他事情:

if df[df['IDnumber'].str[0]] == '2':
   '''Do some stuff'''
if df[df['IDnumber'].str.startswith('2')] == True:
    '''Do some stuff'''

其中分别给出错误:

KeyError: "['2' '2' '2' ..., '1' '1' '1'] not in index"
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

您的意思是要过滤字符串列中的值以某个字符开头的行吗?

>>> df
   foobar
0    0foo
1    1foo
2    2foo
3    3foo
4    4foo
5    5foo
6    0bar
7    1bar
8    2bar
9    3bar
10   4bar
11   5bar
>>> df.loc[(df.foobar.str.startswith('2'))]
  foobar
2   2foo
8   2bar

然后是:

>>> begining_with_2 = df.loc[(df.foobar.str.startswith('2'))]
>>> for i, row in begining_with_2.iterrows():
...    print(row.foobar)
2foo
2bar

不确定这是否会更快,但是...

试试这个:

for x in range(len(df)):
  if df.loc[x,"IDnumber"][0]] == "2":
    '''Do the stuff you want'''

最新更新