rwa :
I have a dataframe like this. I need to insert several empty columns starting at index 1. The code below works, but I am trying to find a way to combine the 4 lines like this:
df.insert(loc=n, column='x', value='')
Into one line, does anyone know a way to do this? Thanks
d = {'one' : pd.Series([10, 20, 30, 40],),
'two' : pd.Series([10, 20, 30, 40],),
'three' : pd.Series([10, 20, 30, 40],)}
df = pd.DataFrame(d)
df.insert(loc=1, column='A', value='')
df.insert(loc=2, column='B', value='')
df.insert(loc=3, column='C', value='')
df.insert(loc=4, column='D', value='')
df
jezrael :
Use concat
with new helper DataFrame
:
i = 1
df = pd.concat([df.iloc[:, :i],
pd.DataFrame('', columns=['A','B','C','D'], index=df.index),
df.iloc[:, i:]], axis=1)
print (df)
one A B C D two three
0 10 10 10
1 20 20 20
2 30 30 30
3 40 40 40
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=220461&siteId=1