Issue
I need to replace column names of a pandas DataFrame having names like 'real_tag' and rename them like 'Descripcion'
list = [{'real_tag': 'FA0:4AIS0007', 'Descripcion': 'velocidad burbujas celda 7'}, {'real_tag': 'FA0:4FIC0116_PVLOLM', 'Descripcion': 'LIMITE BAJO FLUJO AIRE CELDA 2 FLOT. A0'}]
Is there any way I can achieve this? Names need to match...
Solution
Create a dictionary mapping old to new names, then use DataFrame.rename
.
Setup
>>> lst = [{'real_tag': 'FA0:4AIS0007', 'Descripcion': 'velocidad burbujas celda 7'}, {'real_tag': 'FA0:4FIC0116_PVLOLM', 'Descripcion': 'LIMITE BAJO FLUJO AIRE CELDA 2 FLOT. A0'}]
>>> df = pd.DataFrame([[1, 2, 3]], columns=['FA0:4AIS0007', 'FA0:4FIC0116_PVLOLM', 'X'])
>>> df
FA0:4AIS0007 FA0:4FIC0116_PVLOLM X
0 1 2 3
Solution
>>> mapping = {d['real_tag']:d['Descripcion'] for d in lst}
>>> df.rename(mapping, axis='columns')
velocidad burbujas celda 7 LIMITE BAJO FLUJO AIRE CELDA 2 FLOT. A0 X
0 1 2 3
Answered By - timgeb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.