1

I am trying to convert a list of Pandas.Dataframe into a list of dictionnaries and trying to use the column "id" as a key for my dic. Not all of my dataframes have an id column, so I am checking before and some id columns are not. However, none of my results are using the id column as a key:

for dataframe in dataframes:
        if 'id' in dataframe.columns:
            cleaned_dic =  dataframe.set_index('id').T.to_dict('dict')
            else: 
            cleaned_dic = dataframe.T.to_dict('dict')

Example result in json

{
  "0": {
    "date": "2020-01-03",
    "id": 9,
    "journal": "Journal of photochemistry and photobiology. B, Biology",
    "title": "Gold nanoparticles synthesized from Euphorbia fischeriana root by green route method alleviates the isoprenaline hydrochloride induced myocardial infarction in rats."
  },
  "1": {
    "date": "01/01/2020",
    "id": 10,
    "journal": "The journal of maternal-fetal & neonatal medicine",
    "title": "Clinical implications of umbilical artery Doppler changes after betamethasone administration"
  },
  "2": {
    "date": "01/01/2020",
    "id": "11",
    "journal": "Journal of back and musculoskeletal rehabilitation",
    "title": "Effects of Topical Application of Betamethasone on Imiquimod-induced Psoriasis-like Skin Inflammation in Mice."
  },
  "3": {
    "date": "01/03/2020",
    "id": "12",
    "journal": "Journal of back and musculoskeletal rehabilitation",
    "title": "Comparison of pressure release, phonophoresis and dry needling in treatment of latent myofascial trigger point of upper trapezius muscle."
  }
}
1
  • Your code and data has nothing to do with the title of the question. Please add code and stack trace of the error you got or correct the title. Commented Jul 21, 2021 at 8:47

2 Answers 2

1

I believe that the .set_index('id') method is failing because it's trying to order the 'id' columns but it finds INTs and STRs.

You should convert the 'id' columns, before using .set_index('id'), to make all the values in that series of the same type, INTs or STRs.

You may try doing something like this to your dataframe before setting the index:

# raising errors
df.astype({'id': 'int64'}, errors='raise')
# ignoring errors
df.astype({'id': 'int64'}, errors='ignore')
Sign up to request clarification or add additional context in comments.

Comments

1

Try using this code:

d = {}
for dataframe in dataframes:
    d[dataframe['id']] = dataframe.to_dict()

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.