在为panda数据框列中保留空值的同时为其分配类型

时间:2018-10-11 21:35:12

标签: python pandas

我有一个熊猫数据框,并想使用to_gbq()将其导入到bigquery中。一些列具有空值,我想保持它们原样而不将空值替换为nan,None或其他字符串。例如,如果将空值替换为0,则使用下面的行。在更改空值的类型时,是否可以保留空值?

 df['ViewersStart']=df['ViewersStart'].fillna(0).astype('int64')

1 个答案:

答案 0 :(得分:0)

使用<head> <meta charset="UTF-8"> <link rel="apple-touch-icon" sizes="152x152" href="<%=asset_url 'apple-touch-icon'%>"> <link rel="mask-icon" href="<%=asset_url 'safari-pinned-tab.svg'%>" color="#ffc40d"> <meta name="theme-color" content="#ffffff"> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <script src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <style>@import url('https://fonts.googleapis.com/css?family=Reenie+Beanie');</style> <style>@import url('https://fonts.googleapis.com/css?family=Raleway:300,400,500');</style> <%=stylesheet_link_tag "application", :media => "all"%> <%=javascript_include_tag "application"%> <%=csrf_meta_tags%> </head> %=stylesheet_link_tag "custom/forms", :media => "all"%> <%=stylesheet_link_tag "custom/library", :media => "all"%> <%=stylesheet_link_tag "custom/home", :media => "all"%> <%=javascript_include_tag "materialize", :media => "all"%> <%=javascript_include_tag "materialize-form", :media => "all"%> ,仅替换loc不为null的地方:

ViewersStart

示例

df.loc[df.ViewersStart.notnull(),'ViewersStart'] = df.loc[df.ViewersStart.notnull(),'ViewersStart'].astype('int64')