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

Add element in the beginning of a list which extracted from a dataframe

I wanted to add an element at the beginning of a list which is extracted from a pandas dataframe. Below is my example.

import pandas as pd
dat = pd.DataFrame({'X' : [1,2], 'Y' : ['A', 'B']})
[999] + dat['X'].values
### array([1000, 1001])

I wanted to add element 999 at the beginning but python is adding that value to each element.

Can you please help me to find the approach?

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

>Solution :

you can use pd.concat() for data frame column or list.insert() for a simple list, list.insert() takes the index where you want to insert your new value as the first argument and the value as the second argument.
pd.concat() takes the value you want to insert then the column of the data frame if you want it to be inserted at the beginning it must at this order.

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