gpt4 book ai didi

python - fillna 与 namedtuple 或任何其他类 Pandas

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:49 24 4
gpt4 key购买 nike

有什么方法可以在 python 中使用 namedtuple 来填充 na 吗?

我收到此TypeError:

from collections import namedtuple
import pandas as pd
import numpy as np

df = pd.DataFrame([0, 0, 0, 0, np.nan, 0, 0, 0])

nametup = namedtuple('mynp', ['arg1', 'arg2'])
q = nametup(None, None)
df.fillna(q)

Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-25-363ec560dd77>", line 9, in <module>
df.fillna(q)
File "C:\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2762, in fillna
downcast=downcast, **kwargs)
File "C:\Anaconda2\lib\site-packages\pandas\core\generic.py", line 3101, in fillna
'you passed a "{0}"'.format(type(value).__name__))
TypeError: "value" parameter must be a scalar or dict, but you passed a "mynp"

也试过这个:

df.replace(np.nan, q)
Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-31-6f8a86f11bbb>", line 1, in <module>
df.replace(np.nan, q)
File "C:\Anaconda2\lib\site-packages\pandas\core\generic.py", line 3440, in replace
raise TypeError(msg) # pragma: no cover
TypeError: Invalid "to_replace" type: 'float'

任何解决方法?谢谢!

最佳答案

这并不容易,但需要通过对象创建Series,然后替换NaN:

nametup = namedtuple('mynp', ['arg1', 'arg2'])
q = nametup(None, None)

s = pd.Series([q]*len(df.index))
print (s)
0 (None, None)
1 (None, None)
2 (None, None)
3 (None, None)
4 (None, None)
5 (None, None)
6 (None, None)
7 (None, None)
dtype: object

解决方案 mask :

df[0] = df[0].mask(df[0].isnull(), s)
print (df)
0
0 0
1 0
2 0
3 0
4 (None, None)
5 0
6 0
7 0

另一种解决方案 combine_firstfillna系列s:

df[0] = df[0].combine_first(s)
#similar solution
#df[0] = df[0].fillna(s)
print (df)
0
0 0
1 0
2 0
3 0
4 (None, None)
5 0
6 0
7 0

关于python - fillna 与 namedtuple 或任何其他类 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com