Reputation: 768
I have an array:
([ 137.55021238, 125.30017675, 130.20181675, 109.47348838])
I need the array values to replace the b column, with the index number remaining the same:
Index a b
0 0.671399 Nan
35 0.446172 Nan
63 0.614758 Nan
72 0.634448 Nan
I tried to use replace but it didn't work. Is there another way of doing this without turning array into a dataframe and merging?
Upvotes: 4
Views: 17011
Reputation: 403130
vals = [137.55021238, 125.30017675, 130.20181675, 109.47348838]
Option 1
Direct assignment.
df['b'] = vals
print(df)
a b
Index
0 0.671399 137.550212
35 0.446172 125.300177
63 0.614758 130.201817
72 0.634448 109.473488
Option 2
df.assign
df = df.assign(b=vals)
print(df)
a b
Index
0 0.671399 137.550212
35 0.446172 125.300177
63 0.614758 130.201817
72 0.634448 109.473488
Option 3
df.fillna
df.b = df.b.fillna(pd.Series(vals, index=df.index))
print(df)
a b
Index
0 0.671399 137.550212
35 0.446172 125.300177
63 0.614758 130.201817
72 0.634448 109.473488
If your values are Nan
(string) instead of NaN
(float), you can convert it, using df.replace
:
df = df.replace('Nan', np.nan)
Upvotes: 8