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 column of length longer than dataframe pandas

I have:

df=pd.DataFrame({'col1':[1,2,3,4]})

I want to add a column that is based on a list, but the list is longer than the dataframe

new_col=range(9)
df['new_col']=new_col

I want the original columns to be filled with '', and I get:

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

pd.DataFrame({'col1': {0: 1, 1: 2, 2: 3, 3: 4, 4: '', 5: '', 6: '', 7: '', 8: ''}, 'new_col': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}})

>Solution :

Assuming a range index, you first need to reindex your DataFrame, using fill_value='' if you want empty strings as filler:

# if longer, reindex to add new rows
df = df.reindex(range(max(len(df), len(new_col))), fill_value='')

# add the new column
df['new_col'] = new_col

Output:

  col1  new_col
0    1        0
1    2        1
2    3        2
3    4        3
4             4
5             5
6             6
7             7
8             8

Alternatively, if the new column is not pre-existing, concat:

df = pd.DataFrame({'col1':[1,2,3,4]})

new_col=range(9)
df = (pd.concat([df.astype(str), pd.DataFrame({'new_col': new_col})], axis=1)
        .fillna('')
      )
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