【发布时间】:2019-04-04 10:04:37
【问题描述】:
我在 df 中有一个列(“折扣”),其中每个值都采用其格式:
{u'customer': u'xdawd', u'end': None, u'coupon': {u'object': u'coupon', u'name': u'Black Friday', u'percent_off': None, u'created': 213213, u'times_redeemed': 10, u'amount_off': 2500, u'currency': u'gbp', u'object': u'discount', u'start': 1543327380, u'subscription': u'uiodsjciosdj'}
我想在一个新列中返回 percent_off 值或 amount_off 值(两者中只出现一个),所以我必须获取其值不是 None 的那个。
只是一个在 excel 中的示例: https://i.imgur.com/Dt2fj8i.png
【问题讨论】:
-
你试过把它解析成字符串吗?
python str(value) -
考虑切换到 Python3:它有原生的 Unicode 处理,所以它使很多事情变得简单(而不是破解本地编码)。 [顺便说一句,Python2 很快将不再受支持,并且许多软件包不再支持它]:个人意见:传递给 Python3 所需的时间更少,然后在 Python2 上学习正确(“hacking”)Unicode。
标签: python pandas unicode apply