【问题标题】:replace do work in str but does not work in object dtype替换在 str 中工作但在对象 dtype 中不起作用
【发布时间】:2019-12-16 14:09:21
【问题描述】:
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

但是没用,google了很多次都没有解决办法……

gbpusd['Profit'].sum() TypeError: can only concatenate str (not "int") 到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


    【解决方案1】:

    还需要使用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
    

    【讨论】:

    • 非常感谢,但我遇到了一个新问题;你也可以帮忙吗,我想其他人可能也有这个问题
    • NaN 写入空值。而且,您仍然可以汇总或转换利润。你有什么例外?
    • 我没有例外,数字没有空格,也是NaN。但它应该是数字
    • 某些利润单元格数据类型不是字符串。您需要在替换之前将所有内容转换为字符串。您可以在替换 gbpusd['Profit'] = gbpusd['Profit'].astype(str) 之前添加它
    【解决方案2】:

    您可以在单行中转换为字符串和求和:

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

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 2019-08-11
      • 2012-05-30
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 2014-04-07
      相关资源
      最近更新 更多