简体   繁体   English

按数据框中的位置扩展列

[英]extend a column by location in a data-frame

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 中创建空白空间。所以我得到以下信息:

   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()在我的原始代码中,它是在循环中完成的,这就是为什么我使用位置而不仅仅是c.extend()

I get the following error: 'Series' object has no attribute 'extend'我收到以下错误: 'Series' object has no attribute 'extend'

Use reindex with fill_value parameter set to '' :使用reindex并将fill_value参数设置为''

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         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM