2

I have a dict of dataframes as shown below.

enter image description here

Currently what I am trying to do is replace values in column FR of each of the dataframe and create a new column named unit for each dataframe

Though I have a working code as shown below, not sure whether this is the best way to write it.

dataFramesDict['Cho'] = dataFramesDict['Cho'].replace({'FR' : {'Fasting' : 'Cholesterol Fasting', 'Random' : 'Cholesterol Random'}})
dataFramesDict['HDL'] = dataFramesDict['HDL'].replace({'FR' : {'Fasting' : 'Plasma fasting HDL cholesterol measurement', 'Random' : 'Plasma random HDL cholesterol measurement'}})
dataFramesDict['LDL'] = dataFramesDict['LDL'].replace({'FR' : {'Fasting' : 'Plasma fasting LDL cholesterol measurement', 'Random' : 'Plasma random LDL cholesterol measurement'}})
dataFramesDict['Tri'] = dataFramesDict['Tri'].replace({'FR' : {'Fasting' : 'Plasma fasting triglyceride measurement', 'Random' : 'Plasma random triglyceride measurement'}})
dataFramesDict['Cho']['unit'] = 'ml'
dataFramesDict['HDL']['unit'] = 'ml'
dataFramesDict['LDL']['unit'] = 'ml'
dataFramesDict['Tri']['unit'] = 'ml'

Please note the values to replace differs for each dataframe. However the original value is same across all dataframe as can be seen from the code

Can you let me know on how I can improve this further ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
The Great
  • 7,215
  • 7
  • 40
  • 128
  • 1
    did you try to use `for`-loop to create new column ? `for key in dataFramesDict: dataFrameDict[key]['unit'] = 'ml'`. If you have values to replacement in list/dictionary then you could also try to use `for`-loop. – furas Jul 30 '19 at 03:45
  • Would you mind writing it as an answer along with for loop for replace values, then I can mark it as a solution – The Great Jul 30 '19 at 03:53
  • As the replace values were different for each df, I wasn't sure whether for loop would be appropriate – The Great Jul 30 '19 at 03:55
  • you can use `for` loop for replacement if you have it in dictionary which have information in which dataframe use values. – furas Jul 30 '19 at 03:56

1 Answers1

2

You can use for loop to add column

for key in dataFramesDict:
    dataFramesDict[key]['unit'] = 'ml'

or even

for df in dataFramesDict.values():
    df['unit'] = 'ml'

If you have replacements as dictionary then you can use loop too

replacements =  {  
    'Cho': {'FR' : {'Fasting' : 'Cholesterol Fasting', 'Random' : 'Cholesterol Random'}},
    'HDL': {'FR' : {'Fasting' : 'Plasma fasting HDL cholesterol measurement', 'Random' : 'Plasma random HDL cholesterol measurement'}},
    'LDL': {'FR' : {'Fasting' : 'Plasma fasting LDL cholesterol measurement', 'Random' : 'Plasma random LDL cholesterol measurement'}},
    'Tri': {'FR' : {'Fasting' : 'Plasma fasting triglyceride measurement', 'Random' : 'Plasma random triglyceride measurement'}},)
}

for key, value in replacements.items():
    dataFramesDict[key] = dataFramesDict[key].replace(value)

But if you don't have dictionary then you can't change code.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Can you help me do replace using for loop? I am not sure how to generalize it for each dataframe when the values are different for each dataframe – The Great Jul 30 '19 at 04:01
  • Oh. Okay, I was expecting something with string matching without having to create replacement dictionary. Nonetheless your answer provided me a direction, so marking it as solution – The Great Jul 30 '19 at 04:08
  • 1
    see example in answer. But you can't generalize it if you don't have dictionary. And it gets only a bit shorter then older version – furas Jul 30 '19 at 04:08
  • can you help me with this post ? https://stackoverflow.com/questions/57285680/dont-drop-all-nas-during-stack-operation-in-python – The Great Jul 31 '19 at 08:15