简体   繁体   English

将列添加到没有标题的pandas

[英]Add column to pandas without headers

How does one append a column of constant values to a pandas dataframe without headers? 如何将一列常量值附加到没有标题的pandas数据帧? I want to append the column at the end. 我想在最后添加列。

With headers I can do it this way: 随着头我能做到这样的方法:

df['new'] = pd.Series([0 for x in range(len(df.index))], index=df.index)

Each not empty DataFrame has columns , index and some values. 每个非空的DataFrame都有columnsindex和一些值。

You can add default column value and create new column filled by scalar: 您可以添加默认列值并创建由标量填充的新列:

df[len(df.columns)] = 0

Sample: 样品:

df = pd.DataFrame({0:[1,2,3],
                   1:[4,5,6]})

print (df)
   0  1
0  1  4
1  2  5
2  3  6

df[len(df.columns)] = 0
print (df)
   0  1  2
0  1  4  0
1  2  5  0
2  3  6  0

Also for creating new column with name the simpliest is: 另外,对于使用名称创建新列,最简单的是:

df['new'] = 1

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

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