0

I have a pandas DataFrame whose values I want to conditionally change into strings without looping over every value.

Example input:

In [1]: df = pd.DataFrame(data = [[1,2], [4,5]], columns = ['a', 'b'])
Out[2]:
   a  b
0  1  2
1  4  5

This is my best attempt which doesn't work properly

df['a'] = np.where(df['a'] < 3, f'string-{df["a"]}', df['a'])

In [1]: df
Out[2]:
    a                                       b
0   string0 1\n1 4\nName: a, dtype: int64   2
1   4                                       5

Desired output:

Out[2]:
   A         B
0  string-1  2
1  4         5

I am using np.where() since looping is not feasible due to the size of the actual DataFrame. The actual f-string I am using is also more complex and has two variables that include column names, but the problem is the same.

Are there other ways to conditionally change pandas values into f-strings without looping over each value?

1 Answers1

2

You can use .map() together with f-string, as follows:

df['a'] = df['a'].map(lambda x: f'string-{x}' if x < 3 else x)

Alternatively, you can also use .loc together with string concatenation, as follows:

df.loc[df['a'] < 3, 'a'] = 'string-' + df['a'].astype(str)
#OR
df['a']=np.where(df['a'] < 3, 'string-'+df['a'].astype(str), df['a'])

Result:

print(df)

          a  b
0  string-1  2
1         4  5
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
SeaBean
  • 22,547
  • 3
  • 13
  • 25