简体   繁体   English

如何在for循环内的pandas数据框中创建其他列

[英]How to create additional columns in a pandas dataframe inside a for loop

I am working with pandas and would like to add columns to my dataframe from a list. 我正在使用熊猫,并且想从列表中向我的数据框添加列。 Ideally I would like to iterate through my list in a for loop creating a single column in each pass. 理想情况下,我想遍历for循环中的列表,并在每次遍历中创建单个列。

Example: 例:

import pandas as pd

d = {
'name':['Ken','Bobby'],
'age':[5,6],
'score':[1,2]}

df = pd.DataFrame(d,columns=['name','age','score'])

new_columns = ['col1', 'col2']

Output: 输出:

    name    age     score
    Ken     5       1
    Bobby   6       2

Desired output: 所需的输出:

    name    age     score   col1     col2
    Ken     5       1       1        1
    Bobby   6       2       2        2

Corrected solution: 更正的解决方案:

for i in new_columns:
     df[i] = pd.Series([1,2])

Edit: 编辑:

I have corrected the code to fix a typo however there is a great additional solution that does not use for loops which I intend to use in the future. 我已经更正了代码,以解决输入错误,但是还有很多其他解决方案,这些解决方案不用于将来打算使用的循环。

One way of doing that without for loop , assign 一种没有for循环的方法, assign

df=df.assign(**dict.fromkeys(new_columns,[1,2]))
df
Out[84]: 
    name  age  score  col1  col2
0    Ken    5      1     1     1
1  Bobby    6      2     2     2

Also you do not need Series when creating the new columns 另外,在创建新列时不需要Series

for i in new_columns:
     df[i] = [1,2]

df
Out[86]: 
    name  age  score  col1  col2
0    Ken    5      1     1     1
1  Bobby    6      2     2     2

Notice , Personally not recommend using Series to assign , since pandas is index sensitive , which means when your dataframe index is not from range 0 - n , the assign will fail. 注意,个人不建议使用Series进行分配,因为pandas是索引敏感的,这意味着当您的数据帧索引不在0-n范围内时,分配将失败。 For example 例如

df.index=[100,101]
for i in new_columns:
     df[i] = pd.Series([1,2])

df
Out[89]: 
      name  age  score  col1  col2
100    Ken    5      1   NaN   NaN
101  Bobby    6      2   NaN   NaN

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

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