首页 > 解决方案 > 替换在 str 中工作但在对象 dtype 中不起作用

问题描述

ab = '1 234'
ab = ab.replace(" ", "")
ab
'1234'

它很容易replace()摆脱空白,但是当我有一列熊猫数据框时;

gbpusd['Profit'] = gbpusd['Profit'].replace(" ", "")
gbpusd['Profit'].head()
3     7 000.00
4     6 552.00
11    4 680.00
14    3 250.00
24    1 700.00
Name: Profit, dtype: object

但它没有用,谷歌搜索了很多次,但没有解决方案......

gbpusd['Profit'].sum() TypeError: can only concatenate str (not "int") to str

然后,由于空白仍然在这里,无法进行进一步分析,就像sum() 事情比我想象的要难:原始数据是

gbpusd.head()
    Ticket      Open Time           Type    Volume  Item    Price   S / L   T / P   Close Time              Price.1 Commission  Taxes   Swap    Profit
84  50204109.0  2019.10.24 09:56:32 buy     0.5     gbpusd  1.29148 0.0     0.0     2019.10.24 09:57:48     1.29179 0           0.0     0.0     15.5
85  50205025.0  2019.10.24 10:10:13 buy     0.5     gbpusd  1.29328 0.0     0.0     2019.10.24 15:57:02     1.29181 0           0.0     0.0     -73.5
86  50207371.0  2019.10.24 10:34:10 buy     0.5     gbpusd  1.29236 0.0     0.0     2019.10.24 15:57:18     1.29197 0           0.0     0.0     -19.5
87  50207747.0  2019.10.24 10:40:32 buy     0.5     gbpusd  1.29151 0.0     0.0     2019.10.24 15:57:24     1.29223 0           0.0     0.0     36
88  50212252.0  2019.10.24 11:47:14 buy     1.5     gbpusd  1.28894 0.0     0.0     2019.10.24 15:57:12     1.29181 0           0.0     0.0     430.5   

当我这样做的时候

gbpusd['Profit'] = gbpusd['Profit'].str.replace(" ", "")
gbpusd['Profit']
84          NaN
85          NaN
86          NaN
87          NaN
88          NaN
89          NaN
90          NaN
91          NaN
92          NaN
93          NaN
94          NaN
95          NaN
96          NaN
97          NaN
98          NaN
99          NaN
100         NaN
101         NaN
102         NaN
103         NaN
104         NaN
105         NaN
106         NaN
107         NaN
108         NaN
109         NaN
110         NaN
111         NaN
112         NaN
113         NaN
         ...   
117     4680.00
118         NaN
119         NaN
120         NaN
121         NaN
122         NaN
123         NaN
124         NaN
125         NaN
126         NaN
127         NaN
128         NaN
129         NaN
130    -2279.00
131    -2217.00
132    -2037.00
133    -5379.00
134    -1620.00
135    -7154.00
136    -4160.00
137     1144.00
138         NaN
139         NaN
140         NaN
141    -1920.00
142     7000.00
143     3250.00
144         NaN
145     1700.00
146         NaN
Name: Profit, Length: 63, dtype: object

空格被替换了,但是一些没有空格的数据现在是NaN......有人可能有同样的问题......

标签: python

解决方案


也需要使用str

gbpusdprofit = gbpusd['Profit'].str.replace(" ", "")

输出:

0    7000.00
1    6552.00
2    4680.00
3    3250.00
4    1700.00
Name: Profit, dtype: object

总和:

gbpusd['Profit'].str.replace(" ", "").astype('float').sum()

结果:

23182.0

推荐阅读