Pandas Python Regex : error: nothing to repeat

前端 未结 3 406
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 01:10

I have a dataframe with a couple of strange characters, \"*\" and \"-\".

import pandas as pd
import numpy as np

data = {\'year\': [2010, 2011, 2012, 2011,          


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-14 01:40

    You could use a list comprehension within a dict comprehension to do this

    >>> {key: [i if i not in {'*','-'} else '0.00' for i in values] for key, values in data.items()}
    {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
     'wins': [11, '0.00', 10, '0.00', 11, 6, 10, 4],
     'losses': [5, 8, 6, 1, 5, 10, 6, 12],
     'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions']}
    

    This would be done to clean up data before you make a DataFrame.

提交回复
热议问题