仅将值添加到pandas数据框中的单个列

时间:2018-11-15 10:49:09

标签: python pandas dataframe

我有一个数据框,我只需要向特定列添加数据

DF

A  B  C
1  2  3
2  3  4
a  d  f
22 3  3

输出:

A  B  C
1  2  3
2  3  4
a  d  f
22 3  3
32
      34

我尝试过:df['A'].append(pd.DataFrame([valuetoadd]), ignore_index=True),其中valuetoadd是变量

2 个答案:

答案 0 :(得分:2)

您可以将pandas.DataFrame.append与字典(或字典列表,每行一个)一起使用:

>> df.append({'A':32}, ignore_index=True)

    A    B    C
0   1    2    3
1   2    3    4
2   a    d    f
3  22    3    3
4  32  NaN  NaN


>> df.append([{'A':32}, {'C':34}], ignore_index=True)

     A    B    C
0    1    2    3
1    2    3    4
2    a    d    f
3   22    3    3
4   32  NaN  NaN
5  NaN  NaN   34

答案 1 :(得分:0)

您可以将pandas.DataFrame.append与字典(或字典列表,每行一个)一起使用:

df=df.append({'a':variable1}, ignore_index=True)
df=df.append({'a':variable1,'b':variable2}, ignore_index=True)