crompnk
asked on
Reset index in Python Pandas DataFrame
Hi,
I have some json data that I'm converting to a pandas DataFrame. I'd like to make the current index column the 'code' column and reset the index, so the output would be:
code parameters units
1 1 Temperature C
2 10 Specific Conductivity NaN
3 11 Resistivity NaN
4 113 NaN ppm
5 114 NaN ppt
6 117 NaN mg/L
This is my example python code:
I have some json data that I'm converting to a pandas DataFrame. I'd like to make the current index column the 'code' column and reset the index, so the output would be:
code parameters units
1 1 Temperature C
2 10 Specific Conductivity NaN
3 11 Resistivity NaN
4 113 NaN ppm
5 114 NaN ppt
6 117 NaN mg/L
This is my example python code:
import pandas as pd
import json
import requests
data = {
'parameters':{
'22':'NO₃⁻',
'23':'NH₄⁺',
'24':'Cl⁻',
'25':'Turbidity',
'26':'Battery Voltage',
'49':'Velocity',
'28':'Flow Rate',
'29':'Total Flow (volume)',
'flowVelocity':'Flow Velocity',
'50':'Chl-a Concentration',
'51':'Chl-a Fluorescence',
'30':'Partial Pressure O₂',
'31':'Total Suspended Solids',
'54':'BGA-PC Concentration',
'32':'External Voltage',
'10':'Specific Conductivity',
'55':'BGA-PC Fluorescence',
'33':'Battery Level',
'11':'Resistivity',
'34':'RWT Concentration',
'12':'Salinity',
'35':'RWT Fluorescence',
'13':'Total Dissolved Solids',
'58':'BGA-PE Concentration',
'36':'Cl⁻ mV',
'14':'Density',
'59':'BGA-PE Fluorescence',
'density':'Density',
'37':'NO₃⁻ as N',
'16':'Baro',
'38':'NO₃⁻ mV',
'39':'NH₄⁺ as N',
'17':'pH',
'18':'pH MV',
'19':'ORP',
'concentration':'Concentration',
'1':'Temperature',
'2':'Pressure',
'3':'Depth',
'4':'Level: Depth to Water',
'5':'Level: Elevation',
'9':'Actual Conductivity',
'40':'NH₄⁺ mV',
'41':'NH₃ as N',
'20':'DO',
'42':'Σ NH₃',
'21':'% Saturation O₂'
},
'units':{
'megaliters':'ML',
'yard':'yd',
'acreInchPerMinute':'ac-in/min',
'g/mL':'g/mL',
'acreFeetPerSecond':'ac-ft/sec',
'kilolitersPerMinute':'kL/min',
'milliter':'mL',
'193':'FNU',
'194':'NTU',
'litersPerDay':'L/d',
'megalitersPerDay':'ML/d',
'230':'ac-ft',
'274':'mL/hr',
'275':'L/min',
'276':'L/hr',
'millilitersPerSecond':'mL/s',
'113':'ppm',
'114':'ppt',
'acreInchPerDay':'ac-in/d',
'117':'mg/L',
'118':'µg/L',
'megalitersPerMinute':'ML/min',
'97':'psu',
'acreInchPerHour':'ac-in/hr',
'17':'psi',
'liter':'L',
'19':'kPa',
'acreFeetPerMinute':'ac-ft/min',
'kilolitersPerSecond':'kL/sec',
'acreInch':'ac-in',
'162':'mV',
'163':'V',
'241':'%',
'120':'g/L',
'121':'ppb',
'1':'C',
'2':'F',
'129':'g/cm³',
'kilolitersPerDay':'kL/d',
'megalitersPerSecond':'ML/sec',
'209':'ft³/s',
'20':'bar',
'21':'mbar',
'65':'µS/cm',
'66':'mS/cm',
'22':'mm Hg',
'23':'in Hg',
'24':'cm H₂O',
'megalitersPerHour':'ML/hr',
'26':'torr',
'27':'atm',
'kiloliters':'kL',
'177':'% sat',
'210':'ft³/min',
'211':'ft³/hr',
'212':'ft³/day',
'kilolitersPerHour':'kL/hr',
'257':'RFU',
'213':'gal/s',
'214':'gal/min',
'215':'gal/hr',
'216':'Mgal/day',
'337':'ft/s',
'338':'m/s',
'217':'m³/s',
'218':'m³/min',
'219':'m³/hr',
'millilitersPerDay':'mL/day',
'33':'mm',
'34':'cm',
'35':'m',
'37':'in',
'38':'ft',
'acreInchPerSecond':'ac-in/sec',
'acreFeetPerHour':'ac-ft/hr',
'220':'m³/day',
'221':'L/s',
'222':'ac-ft/d',
'145':'pH',
'223':'mL/min',
'225':'ft³',
'226':'gal',
'227':'Mgal',
'228':'m³',
'81':'Ω-cm',
'inH2o':'in H₂O'
}
}
val = json.loads(json.dumps(data))
df = pd.DataFrame.from_dict(val, orient='columns')
df.columns = ['parameters', 'units']
print(df)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.