首页 > 解决方案 > 如何使所有行数据删除相似数据并乘以浮点数

问题描述

如何分隔所有列?

df= df[['hlogUs_dB','hlogDs_dB']]
df

    hlogUs_dB                                              hlogDs_dB
    0   109:-3.4,110:-3.4,111:-3.4,112:-3.5,113:-3.5,1...   5:-2.5,6:-2.5,7:-2.1,8:-2.0,9:-2.0,10:-2.0,11:...
    1   109:-3.5,110:-3.5,111:-3.4,112:-3.4,113:-3.4,1...   5:-2.1,6:-2.0,7:-1.8,8:-1.8,9:-1.8,10:-1.8,11:...
    2   109:-3.7,110:-3.7,111:-3.8,112:-3.8,113:-3.8,1...   5:-2.1,6:-2.0,7:-1.8,8:-1.8,9:-1.8,10:-1.8,11:...
    3   109:-3.5,110:-3.6,111:-3.6,112:-3.6,113:-3.7,1...   5:-2.5,6:-2.5,7:-2.1,8:-2.0,9:-2.0,10:-2.0,11:...
    4   109:-3.7,110:-3.8,111:-3.8,112:-3.8,113:-3.8,1...   5:-2.5,6:-2.5,7:-2.1,8:-2.1,9:-2.0,10:-2.1,11:...
    ... ... ...
    165 109:-5.2,110:-5.3,111:-5.5,112:-5.7,113:-5.9,1...   5:-2.5,6:-2.5,7:-2.1,8:-2.1,9:-2.1,10:-2.2,11:...
    166 109:-5.5,110:-5.6,111:-5.8,112:-6.1,113:-6.3,1...   5:-2.8,6:-2.7,7:-2.5,8:-2.5,9:-2.3,10:-2.5,11:...
    167 109:-6.0,110:-6.2,111:-6.4,112:-6.7,113:-7.1,1...   5:-2.6,6:-2.5,7:-2.2,8:-2.2,9:-2.2,10:-2.3,11:...
    168 109:-5.4,110:-5.5,111:-5.7,112:-5.9,113:-6.2,1...   5:-3.0,6:-3.0,7:-2.6,8:-2.5,9:-2.5,10:-2.5,11:...
    169 109:-5.9,110:-6.1,111:-6.4,112:-6.6,113:-7.0,1...   5:-2.7,6:-2.5,7:-2.3,8:-2.2,9:-2.3,10:-2.3,11:...
    170 rows × 2 columns

<之后我只使用分隔符拆分hlogUs_dB/>

df2 =df['hlogUs_dB'].str.split('[,:]',expand = True)
    df2 = data.drop(["0"])
    df2


 

结果 :

    0   1   2   3   4   5   6   7   8   9   ... 276 277 278 279 280 281 282 283 284 285
0   109 -3.4    110 -3.4    111 -3.4    112 -3.5    113 -3.5    ... 343 -4.3    344 -4.3    345 -4.2    346 -4.2    347 -4.2
1   109 -3.5    110 -3.5    111 -3.4    112 -3.4    113 -3.4    ... 343 -4.1    344 -4.2    345 -4.4    346 -4.4    347 -4.2
2   109 -3.7    110 -3.7    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.3    345 -4.3    346 -4.3    347 -4.3
3   109 -3.5    110 -3.6    111 -3.6    112 -3.6    113 -3.7    ... 343 -4.1    344 -4.1    345 -4.1    346 -4.1    347 -4.1
4   109 -3.7    110 -3.8    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.2    345 -4.2    346 -4.2    347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2    110 -5.3    111 -5.5    112 -5.7    113 -5.9    ... 343 -5.4    344 -5.3    345 -5.2    346 -5.1    347 -5.1
166 109 -5.5    110 -5.6    111 -5.8    112 -6.1    113 -6.3    ... 343 -5.5    344 -5.4    345 -5.3    346 -5.2    347 -5.2
167 109 -6.0    110 -6.2    111 -6.4    112 -6.7    113 -7.1    ... 343 -4.9    344 -4.9    345 -4.9    346 -4.9    347 -4.9
168 109 -5.4    110 -5.5    111 -5.7    112 -5.9    113 -6.2    ... 343 -5.9    344 -5.7    345 -5.7    346 -5.6    347 -5.6
169 109 -5.9    110 -6.1    111 -6.4    112 -6.6    113 -7.0    ... 343 -5.7    344 -5.7    345 -5.7    346 -5.6    347 -5.6
170 rows × 286 columns

之后,我想删除仅出现在偶数列上的相同数字。我设法找到了解决方案,但不知何故或更确切地说,它不符合我的喜好。

df2.drop(columns=[0,2,4,6,8,9,10,12,14,16,18,20,22,24,26,28,30,32,34,36])
df2

输出:

0   1   2   3   4   5   6   7   8   9   ... 276 277 278 279 280 281 282 283 284 285
0   109 -3.4    110 -3.4    111 -3.4    112 -3.5    113 -3.5    ... 343 -4.3    344 -4.3    345 -4.2    346 -4.2    347 -4.2
1   109 -3.5    110 -3.5    111 -3.4    112 -3.4    113 -3.4    ... 343 -4.1    344 -4.2    345 -4.4    346 -4.4    347 -4.2
2   109 -3.7    110 -3.7    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.3    345 -4.3    346 -4.3    347 -4.3
3   109 -3.5    110 -3.6    111 -3.6    112 -3.6    113 -3.7    ... 343 -4.1    344 -4.1    345 -4.1    346 -4.1    347 -4.1
4   109 -3.7    110 -3.8    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.2    345 -4.2    346 -4.2    347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2    110 -5.3    111 -5.5    112 -5.7    113 -5.9    ... 343 -5.4    344 -5.3    345 -5.2    346 -5.1    347 -5.1
166 109 -5.5    110 -5.6    111 -5.8    112 -6.1    113 -6.3    ... 343 -5.5    344 -5.4    345 -5.3    346 -5.2    347 -5.2
167 109 -6.0    110 -6.2    111 -6.4    112 -6.7    113 -7.1    ... 343 -4.9    344 -4.9    345 -4.9    346 -4.9    347 -4.9
168 109 -5.4    110 -5.5    111 -5.7    112 -5.9    113 -6.2    ... 343 -5.9    344 -5.7    345 -5.7    346 -5.6    347 -5.6
169 109 -5.9    110 -6.1    111 -6.4    112 -6.6    113 -7.0    ... 343 -5.7    344 -5.7    345 -5.7    346 -5.6    347 -5.6
170 rows × 286 columns

仍然显示与以前相同,我只希望奇数列与 8 和float4.3125 相乘。然后稍后数据将在相同的列上替换,这是我的大致想法。

df2*4.3125

结果包含错误。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_arithmetic_op(left, right, op, str_rep)
    148     try:
--> 149         result = expressions.evaluate(op, str_rep, left, right)
    150     except TypeError:

~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr)
    207     if use_numexpr:
--> 208         return _evaluate(op, op_str, a, b)
    209     return _evaluate_standard(op, op_str, a, b)

~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b)
    120     if result is None:
--> 121         result = _evaluate_standard(op, op_str, a, b)
    122 

~\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b)
     69     with np.errstate(all="ignore"):
---> 70         return op(a, b)
     71 

TypeError: can't multiply sequence by non-int of type 'float'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-24-424060d3aad6> in <module>
----> 1 df2*4.3125

~\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in f(self, other, axis, level, fill_value)
    717                 self = self.fillna(fill_value)
    718 
--> 719             new_data = dispatch_to_series(self, other, op, str_rep)
    720             return self._construct_result(new_data)
    721 

~\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in dispatch_to_series(left, right, func, str_rep, axis)
    376         # Get the appropriate array-op to apply to each block's values.
    377         array_op = get_array_op(func, str_rep=str_rep)
--> 378         bm = left._data.apply(array_op, right=right)
    379         return type(left)(bm)
    380 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, filter, **kwargs)
    438 
    439             if callable(f):
--> 440                 applied = b.apply(f, **kwargs)
    441             else:
    442                 applied = getattr(b, f)(**kwargs)

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in apply(self, func, **kwargs)
    388         """
    389         with np.errstate(all="ignore"):
--> 390             result = func(self.values, **kwargs)
    391 
    392         if is_extension_array_dtype(result) and result.ndim > 1:

~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in arithmetic_op(left, right, op, str_rep)
    195     else:
    196         with np.errstate(all="ignore"):
--> 197             res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
    198 
    199     return res_values

~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_arithmetic_op(left, right, op, str_rep)
    149         result = expressions.evaluate(op, str_rep, left, right)
    150     except TypeError:
--> 151         result = masked_arith_op(left, right, op)
    152 
    153     return missing.dispatch_fill_zeros(op, left, right, result)

~\anaconda3\lib\site-packages\pandas\core\ops\array_ops.py in masked_arith_op(x, y, op)
    110         if mask.any():
    111             with np.errstate(all="ignore"):
--> 112                 result[mask] = op(xrav[mask], y)
    113 
    114     result, _ = maybe_upcast_putmask(result, ~mask, np.nan)

TypeError: can't multiply sequence by non-int of type 'float'

我被困在该地区,并在 Stack Overflow、youtube 上搜索了关于浮点数乘法的基本知识,但我认为我的关键字与这些想法不符。

标签: pandasdataframedelimiter

解决方案


推荐阅读