adding value to only a single column in pandas dataframe

Viewed 25361

I have a dataframe and i need to add data only to a specific column

DF

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

output :

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

I tried : df['A'].append(pd.DataFrame([valuetoadd]), ignore_index=True) where valuetoadd is a variable

2 Answers

You can use pandas.DataFrame.append with a dictionary (or list of dictionaries, one per row):

>> 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

You can use pandas.DataFrame.append with a dictionary (or list of dictionaries, one per row):

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