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?
CodePudding user response:
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.