1

I am trying to write data into individual cell of my pandas dataframe but am not able to do it.

For example, my initialized dataframe looks like this:

                                  PF44         PF62        PF12         

                                 aa   ss      aa   ss      aa   ss 
1B38:A|ABCDE|DDFE|QUAD           NaN  NaN     NaN  NaN     NaN  NaN   

I can access a particular element of my dataframe using the following command:

>>>feat_df.loc['1B38:A|ABCDE|DDFE|QUAD']['PF44']['ss'] 

and the output is nan. However, if I want to change this to say a number 5 or a text 'test', I used the following commands:

>>>feat_df.loc['1B38:A|ABCDE|DDFE|QUAD']['PF44']['ss'] =5

or

>>>feat_df.loc['1B38:A|ABCDE|DDFE|QUAD']['PF44']['ss']='test'

neither returned any error but when the check the value:

>>>feat_df.loc['1B38:A|ABCDE|DDFE|QUAD']['PF44']['ss'] 

its still shows

nan
3
  • What does feat_df.index and feat_df.columns return? What does feat_df.head(5).to_dict() Output. Add this to you question so we can get a clear structure of your dataframe. Commented Jun 5, 2018 at 19:56
  • It seems like you are modifying the copy of dataframe and not dataframe itself. Does the IDE show any warnings? Commented Jun 5, 2018 at 20:04
  • @HarvIpan no, IDE does not show any warnings Commented Jun 5, 2018 at 20:04

1 Answer 1

1

IIUC, try this:

feat_df.loc['1B38:A|ABCDE|DDFE|QUAD', ('PF44','ss')] = 5

or

feat_df.loc['1B38:A|ABCDE|DDFE|QUAD', ('PF44','ss')] = 'test'

Use tuples to access MultiIndex Column headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.