running man
running man

Reputation: 1467

How to select pandas Series values start with a specific text?

I want to select a specific column's value from Pandas DataFrame which value start with a specific text. If True, the next column will be updated with the content from previous column's value.

For example, I have a DataFrame df_1, and I want a2 update with values that a1 start with as.

df_1 = pd.DataFrame({'a1':['amtr1',np.nan,'astr2',np.nan,'gbstr2','mkstr2','gbstr2','astr1'],
                     'a2':[np.nan]*8})
df_1

    a1      a2
0   amtr1   NaN
1   NaN     NaN
2   astr2   NaN
3   NaN     NaN
4   gbstr2  NaN
5   mkstr2  NaN
6   gbstr2  NaN
7   astr1   NaN

I want this output.

    a1      a2
0   amtr1   NaN
1   NaN     NaN
2   astr2   astr2
3   NaN     NaN
4   gbstr2  NaN
5   mkmk2   NaN
6   gbstr2  NaN
7   astr1   astr1

Upvotes: 0

Views: 321

Answers (1)

EdChum
EdChum

Reputation: 394031

use loc with a boolean condition using vectorised str.startswith with arg 'as' and select column 'a2' and assign column 'a1' values:

In [59]:
df_1.loc[df_1['a1'].str.startswith('as', na=False), 'a2'] = df_1['a1']
df_1

Out[59]:
       a1     a2
0   amtr1    NaN
1     NaN    NaN
2   astr2  astr2
3     NaN    NaN
4  gbstr2    NaN
5  mkstr2    NaN
6  gbstr2    NaN
7   astr1  astr1

Upvotes: 2

Related Questions