0

I'd like to change column name of dataframe:

def test(var):
    dic={'a': [5, 9],
     'b': [4, 1],
     'c': [2, 9]}

    df=pd.DataFrame(dic).transpose()
    df.columns=['aad',var_'k']
    return df
test('lalala')   

I would like second column name as var_'k'; that is, if I passed in lalala, the column name should be: lalala_k. But obviously there's syntax problem. What should I do? Thx

1 Answer 1

4

This is just a normal string; you can build it up with concatenation or string functions. For example:

col = var + '_k'

or

col = '{}_k'.format(var)
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.