【发布时间】:2023-01-28 07:43:51
【问题描述】:
数据框看起来像这样,我想用“年”列中的内容加上“0601”来填充“日期”列中的空单元格(当“区域”为西或北时)。
想要的结果如下:
我试过的:
from io import StringIO
import pandas as pd
csvfile = StringIO(
"""
Name Area Date Year
David West 2014
Mike North 20220919 2022
Kate West 2017
Lilly East 20221226 2022
Peter North 20221226 2022
Cara Middle 2016
""")
df = pd.read_csv(csvfile, sep = '\t', engine='python')
L1 = ['West','North']
m1 = df['Date'].isnull()
m2 = df['Area'].isin(L1)
df['Date'] = df['Date'].mask(m1 & m2, df['Year'] + '0601') # Try_1
df['Date'] = np.where(np.where(m1 & m2, df['Year'] + '0601')) # Try_2
Try_1 和 Try_2 都弹出相同的错误。
台词的正确写法是什么?
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 142, in _na_arithmetic_op
result = expressions.evaluate(op, left, right)
File "C:\Python38\lib\site-packages\pandas\core\computation\expressions.py", line 235, in evaluate
return _evaluate(op, op_str, a, b) # type: ignore[misc]
File "C:\Python38\lib\site-packages\pandas\core\computation\expressions.py", line 69, in _evaluate_standard
return op(a, b)
numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\My Documents\Scripts\(Desktop) WSS 20200323\GG.py", line 336, in <module>
df['Date'] = np.where(np.where(m1 & m2, df['Year'] + '0601')) # try 2
File "C:\Python38\lib\site-packages\pandas\core\ops\common.py", line 65, in new_method
return method(self, other)
File "C:\Python38\lib\site-packages\pandas\core\arraylike.py", line 89, in __add__
return self._arith_method(other, operator.add)
File "C:\Python38\lib\site-packages\pandas\core\series.py", line 4998, in _arith_method
result = ops.arithmetic_op(lvalues, rvalues, op)
File "C:\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 189, in arithmetic_op
res_values = _na_arithmetic_op(lvalues, rvalues, op)
File "C:\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 149, in _na_arithmetic_op
result = _masked_arith_op(left, right, op)
File "C:\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 111, in _masked_arith_op
result[mask] = op(xrav[mask], y)
numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
【问题讨论】:
-
请提供基于字典和您的 python/pandas 版本的 DataFrame 构造函数,如下所示,我无法重现您的问题。