简体   繁体   中英

Efficiently search for first character of a string in a pandas dataframe

I have a pandas data frame column and I need to modify any entry of that column that starts with a 2. Right now, I'm using this which works, but is very, very slow:

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

I feel (read: know) there's a more efficent way to do this without using a for loop but I can't seem to find it.

Other things I've tried:

if df[df['IDnumber'].str[0]] == '2':
   '''Do some stuff'''

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

Which respectively give the errors:

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().

Do you mean you want to filter rows where the value from a string column starts with some character?

>>> 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

Then it is:

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

2foo
2bar

Not sure if this will be faster, but...

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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