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

Pandas assign a pd series with longer len than df, to a col in that df

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
new_series = pd.Series([7, 8,9,10], name='C')
df['B'] = new_series

in this code the column ‘B’ does not get replaced with new series. so I want the df to be

df["B"]=[1, 2, 3,nan]
df["B"]=[7, 8,9,10]

>Solution :

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

Make indices between df and new_series agreed, then – assign the needed column:

df.reindex(new_series.index).assign(B=new_series)

     A   B
0  1.0   7
1  2.0   8
2  3.0   9
3  NaN  10
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