简体   繁体   English

如何使用 iloc 向现有 dataframe(无标题)添加新列?

[英]How to add new column to existing dataframe (no headers) using iloc?

I want to create third column to existing dataframe, having the same values of 2nd column using iloc pandas method.我想为现有的 dataframe 创建第三列,使用 iloc pandas 方法具有与第二列相同的值。 What are the options do I have?我有哪些选择?

df = pd.DataFrame([*zip([1,2,3],[4,5,6])])

here is one way to do it这是一种方法

df.insert(len(df.columns), # position of new column, it points to column after last column
          len(df.columns), # naming could be the location of column
          value=df.iloc[:,1] # grab the values from last column
         )
df

    0   1   2
0   1   4   4
1   2   5   5
2   3   6   6

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

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