-1

I can't figure out how to add element to pandas column for single row.

index = ['a','b','c','d']
df = pd.DataFrame(index = index)
df['Col'] = [[]] * len(index)

df.loc['d','Col'].append('AA')

returns:

    Col
a   [AA]
b   [AA]
c   [AA]
d   [AA]

I look for value 'AA' to be in the last row only.

Ranny
  • 315
  • 2
  • 13

2 Answers2

2

We should create the list of empty list by , avoid same object assignment. Also you can use .loc for assign the value in pandas

df['Col'] = [[] for x in df.index]
df.loc['d','Col']='AA'
df
  Col
a  []
b  []
c  []
d  AA

df.loc['d','Col'].append('AA')
df
    Col
a    []
b    []
c    []
d  [AA]
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Try

df.loc['d','Col'] = 'AA'

This should work for you

Edit:

Based on your edit, maybe this could be more efficient:

df.iloc[-1] = [['AA']]
Thales Marques
  • 333
  • 4
  • 18