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:
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('')
)