Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a ONE-LINER way to give each row of a dataframe a unique id consisting of an integer and string?

This is my DataFrame:

import pandas as pd
df = pd.DataFrame(
    {
        'a': [4, 3, 2, 2, 6]
    }
)

And this is the expected output: I want to create column id:

   a   id
0  4  x_0
1  3  x_1
2  2  x_2
3  2  x_3
4  6  x_4

I can create id like this but I think there is a one-liner for this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['id'] = np.arange(len(df))
df['id'] = 'x_' + df.id.astype(str)

I prefer a solution that does not use index.

>Solution :

If you want a one-liner, you have to convert your array to Series (raw arrays are not practical for string concatenation):

df['id'] = 'x_' + pd.Series(np.arange(len(df)), dtype=str)

# or
df['id'] = pd.Series(np.arange(len(df)), dtype=str).radd('x_')

Output:

   a   id
0  4  x_0
1  3  x_1
2  2  x_2
3  2  x_3
4  6  x_4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading