使用fillna,垂头丧气的熊猫

use*_*451 6 python numpy pandas

我一直在寻找可以帮助我理解downcast类方法中关键字参数的内容DataFrame.fillna。请提供示例以帮助促进我和每个人的学习:http : //pandas.pydata.org/pandas-docs/stable/generation/pandas.DataFrame.fillna.html

另外,如果您可以说出一两个字,关于每一列的类型设置,NaN甚至NoneType是该列中的值,以及如何处理此类常见内容。两者之间的区别是什么。

非常感谢你!

two*_*rec 4

尽管医生说:

\n\n
\n

沮丧字典,默认为 None

\n\n

如果可能的话,要向下转换的 item->dtype 的字典,或者字符串 \xe2\x80\x98infer\xe2\x80\x99 ,它将尝试向下转换\n 到适当的相等类型(例如,如果可能,将 float64 转换为 int64 )

\n
\n\n

如果你提供 dict 作为downcast你会得到AssertionError("dtypes as dict is not supported yet")

\n\n

只能使用一个downcast=\'infer\'导致 pandas 尝试向下转换的方法,例如将浮点数转换为整数。但这似乎有问题:如果列中的所有浮点数都超过 10000,它将失去精度并将它们转换为整数。

\n\n
In [1]: import pandas as pd\n   ...: import numpy as np\n   ...: df = pd.DataFrame([[3.14,9999.9,10000.1,200000.2],[2.72,9999.9,10000.1,300000.3]], columns=list("ABCD"))\n   ...: df.dtypes\n   ...: \nOut[1]: \nA    float64\nB    float64\nC    float64\nD    float64\ndtype: object\n\nIn [2]: df\nOut[2]: \n      A       B        C         D\n0  3.14  9999.9  10000.1  200000.2\n1  2.72  9999.9  10000.1  300000.3\n\nIn [3]: dff=df.fillna(0, downcast=\'infer\')\n   ...: dff.dtypes\n   ...: \nOut[3]: \nA    float64\nB    float64\nC      int64\nD      int64\ndtype: object\n\nIn [4]: dff\nOut[4]: \n      A       B      C       D\n0  3.14  9999.9  10000  200000\n1  2.72  9999.9  10000  300000\n
Run Code Online (Sandbox Code Playgroud)\n

  • 现在已经修复了 (3认同)