0

I have a dataframe like this:

df = pd.DataFrame(columns=['aab', 'aac', 'bba'])

and I would like to add the same value to all columns which start with "aa%" something like this:

df = df.append({'aa%': 'val1', 'bba': 'val2'}, ignore_index=True)

and get this result:

+------+------+------+
| aab  | aac  | bba  |
+------+------+------+
| val1 | val1 | val2 |
+------+------+------+

Is it possible?

2

1 Answer 1

2

First use append to create empty row, then fill empty row with val1 where column names starts with aa. Finally fill rest of values with val2 :

df.append(pd.Series(), ignore_index=True)
df.loc[0, df.columns[df.columns.str.startswith('aa')]] = 'val1'
df.fillna('val2')

result:

    aab   aac   bba
0  val1  val1  val2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.