I have the following indexed DataFrame with named columns and rows not- continuous numbers:
a b c d
2 0.671399 0.101208 -0.181532 0.241273
3 0.446172 -0.243316 0.051767 1.577318
5 0.614758 0.075793 -0.451460 -0.012493
I would like to add a new column, 'e'
, to the existing data frame and do not want to change anything in the data frame (i.e., the new column always has the same length as the DataFrame).
0 -0.335485
1 -1.166658
2 -0.385571
dtype: float64
I tried different versions of join
, append
, merge
, but I did not get the result I wanted, only errors at most. How can I add column e
to the above example?
I was looking for a general way of adding a column of
numpy.nan
s to a dataframe without getting the dumbSettingWithCopyWarning
.From the following:
numpy
array of NaNs in-lineI came up with this:
This is the simple way of adding a new column:
df['e'] = e
To add a new column, 'e', to the existing data frame
It seems that in recent Pandas versions the way to go is to use df.assign:
df1 = df1.assign(e=np.random.randn(sLength))
It doesn't produce
SettingWithCopyWarning
.list_of_e
that has relevant data.df['e'] = list_of_e
For the sake of completeness - yet another solution using DataFrame.eval() method:
Data:
Solution: