问候大家。我有一个 excel 文件,我需要根据列数据类型清理和填充 NaN 值,例如,如果列数据类型是对象,我需要在该列中填充“NULL”,如果数据类型是整数或浮点数,则需要填充 0在那些列中。
到目前为止,我已经尝试了 2 种方法来完成这项工作,但没有运气,这是第一个
df = pd.read_excel("myExcel_files.xlsx")
Run Code Online (Sandbox Code Playgroud)
df.select_dtypes(include='int64').fillna(0, inplace=True)
df.select_dtypes(include='float64').fillna(0.0, inplace=True)
df.select_dtypes(include='object').fillna("NULL", inplace=True)
Run Code Online (Sandbox Code Playgroud)
我得到的输出不是错误而是警告,数据框没有变化
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py:4259: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
**kwargs
Run Code Online (Sandbox Code Playgroud)
df = pd.read_excel("myExcel_files.xlsx")
#get the list of all integer columns
int_cols = list(df.select_dtypes('int64').columns)
#get the list of all float columns
float_cols = list(df.select_dtypes('float64').columns)
#get the list of all object columns
object_cols = …Run Code Online (Sandbox Code Playgroud)