extend a column by location in a data-frame

AspiringCoder :

I want to extend a column , but I don't know the column names in my original data so I want to access it by location and extend it. I have the following code:

import pandas as pd
a=[1,2,3]
b=[1,2,3]
c=[a,b]
c=pd.DataFrame(c)

c[0].extend(['']*len(a))
print(c)

I want to create empty space in column location 0. So I get the following:

   0  1  2
0  1  2  3
1  1  2  3
2
3

In my original code it is done in a loop, that is why I use the location not just c.extend()

I get the following error: 'Series' object has no attribute 'extend'

Chris A :

Use reindex with fill_value parameter set to '':

import numpy as np

a=[1,2,3]
b=[1,2,3]
c=[a,b]
c=pd.DataFrame(c)

c = c.reindex(np.arange(len(c)+len(a)), fill_value='')

[out]

   0  1  2
0  1  2  3
1  1  2  3
2         
3         
4         

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26614&siteId=1