rishabh.kashyap
rishabh.kashyap

Reputation: 61

assign string value to a cell in pandas

I've created a new row for storing mean values of all columns. Now I'm trying to assign name to the very first cell of the new row

I've tried the conventional method of assigning value by pointing to the cell index. It doesn't return any error but it doesn't seems to store the value in the cell.

  Items Description Duration  China   Japan   Korea
0      GDP          2012-2013  40000  35000   12000
1      GDP          2013-2014  45000  37000   12500
2      NAN          NAN        42500  36000   12250

data11.loc[2,'Items Description'] = 'Average GDP'

Instead of returning below dataframe the code is still giving the previous output.

  Items Description Duration  China   Japan   Korea
0      GDP          2012-2013  40000  35000   12000
1      GDP          2013-2014  45000  37000   12500
2  Average GDP      NAN        42500  36000   12250

Upvotes: 4

Views: 6514

Answers (1)

jezrael
jezrael

Reputation: 862751

For me working nice, but here are 2 alternatives for set value by last row and column name.

First is DataFrame.loc with specify last index value by indexing:

data11.loc[data11.index[-1], 'Items Description'] = 'Average GDP'

Or DataFrame.iloc with -1 for get last row and Index.get_loc for get position of column Items Description:

data11.iloc[-1, data11.columns.get_loc('Items Description')] = 'Average GDP'

print (data11)
  Items Description   Duration  China  Japan  Korea
0               GDP  2012-2013  40000  35000  12000
1               GDP  2013-2014  45000  37000  12500
2       Average GDP        NAN  42500  36000  12250

Upvotes: 5

Related Questions