假设您具有以下DF:
In [30]: dfOut[30]: Date Val0 2016-09-23 1001 2016-09-22 9.60M2 2016-09-21 54.20K3 2016-09-20 115.30K4 2016-09-19 18.90K5 2016-09-16 176.10K6 2016-09-15 31.60K7 2016-09-14 10.00K8 2016-09-13 3.20M
您可以这样 *** 作:
In [31]: df.Val = (df.Val.replace(r'[KM]+$', '', regex=True).astype(float) * ....:df.Val.str.extract(r'[d.]+([KM]+)', expand=False) ....: .fillna(1) ....: .replace(['K','M'], [10**3, 10**6]).astype(int))In [32]: dfOut[32]: Date Val0 2016-09-23 100.01 2016-09-22 9600000.02 2016-09-21 54200.03 2016-09-20 115300.04 2016-09-19 18900.05 2016-09-16 176100.06 2016-09-15 31600.07 2016-09-14 10000.08 2016-09-13 3200000.0
说明:
In [36]: df.Val.replace(r'[KM]+$', '', regex=True).astype(float)Out[36]:0 100.01 9.62 54.23 115.34 18.95 176.16 31.67 10.08 3.2Name: Val, dtype: float64In [37]: df.Val.str.extract(r'[d.]+([KM]+)', expand=False)Out[37]:0 NaN1 M2 K3 K4 K5 K6 K7 K8 MName: Val, dtype: objectIn [38]: df.Val.str.extract(r'[d.]+([KM]+)', expand=False).fillna(1)Out[38]:0 11 M2 K3 K4 K5 K6 K7 K8 MName: Val, dtype: objectIn [39]: df.Val.str.extract(r'[d.]+([KM]+)', expand=False).fillna(1).replace(['K','M'], [10**3, 10**6]).astype(int)Out[39]:0 11 10000002 10003 10004 10005 10006 10007 10008 1000000Name: Val, dtype: int32
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)