在本文中,我们将为您详细介绍python–在datetime对象上的pandasfillna的相关知识,并且为您解答关于python里datetime怎么用的疑问,此外,我们还会提供一些关于.fill
在本文中,我们将为您详细介绍python – 在datetime对象上的pandas fillna的相关知识,并且为您解答关于python里datetime怎么用的疑问,此外,我们还会提供一些关于.fillna 打破 .dt.normalize()、.reset_index 之后的 fillna() 用于创建 DataFrame、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、df.fillna() 不会替换所有 NaN 值的有用信息。
本文目录一览:- python – 在datetime对象上的pandas fillna(python里datetime怎么用)
- .fillna 打破 .dt.normalize()
- .reset_index 之后的 fillna() 用于创建 DataFrame
- Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod
- df.fillna() 不会替换所有 NaN 值
python – 在datetime对象上的pandas fillna(python里datetime怎么用)
DF [ ‘日期’].fillna(日期时间( “2000-01-01”))
我明白了:
TypeError:需要一个整数
有什么方法吗?
解决方法
@DSM指出日期时间的构造如下:datetime.datetime(2012,1,1)
所以错误是由于未能构建您传递给fillna的值.
请注意,使用时间戳会解析字符串.
In [3]: s = Series(date_range('20130101',periods=10)) In [4]: s.iloc[3] = pd.NaT In [5]: s.iloc[7] = pd.NaT In [6]: s Out[6]: 0 2013-01-01 00:00:00 1 2013-01-02 00:00:00 2 2013-01-03 00:00:00 3 NaT 4 2013-01-05 00:00:00 5 2013-01-06 00:00:00 6 2013-01-07 00:00:00 7 NaT 8 2013-01-09 00:00:00 9 2013-01-10 00:00:00 dtype: datetime64[ns]
datetime.datetime也可以
In [7]: s.fillna(Timestamp('20120101')) Out[7]: 0 2013-01-01 00:00:00 1 2013-01-02 00:00:00 2 2013-01-03 00:00:00 3 2012-01-01 00:00:00 4 2013-01-05 00:00:00 5 2013-01-06 00:00:00 6 2013-01-07 00:00:00 7 2012-01-01 00:00:00 8 2013-01-09 00:00:00 9 2013-01-10 00:00:00 dtype: datetime64[ns]
.fillna 打破 .dt.normalize()
使用 normalize()
不会改变列的 dtype,pandas 只是在打印时停止显示时间部分,因为它们共享相同的时间部分。
我会推荐正确的解决方案,将列转换为实际的 datetime.date
而不是使用 normalize()
:
df['date'] = pd.to_datetime(df['date']).dt.date
.reset_index 之后的 fillna() 用于创建 DataFrame
如何解决.reset_index 之后的 fillna() 用于创建 DataFrame
我有 3 个 dfs,其中包含一些用户的通话、消息和互联网数据。我使用 groupby 来查找每个用户每月使用的呼叫(或消息,或 GB)数量,然后使用 .reset_index
将 MultiIndexes 转换为 DataFrames。通过进一步的分析,我注意到对于一些用户 id,有 NaN 值,因为有几个月,一些活跃用户没有拨打任何电话、发送任何消息或使用任何数据。为了解决这个问题,我尝试使用 .fillna()
但它不起作用,因此当我为 total_calls 提取具有已知 NaN 值的特定 user_id 时,它会打印一个空数据帧。
我试过了:
calls_mins_per_month.fillna({''duration'':0},inplace=True)
calls_mins_per_month[''duration''] = calls_mins_per_month[''duration''].fillna(0)
calls_mins_per_month[''duration''].fillna(0,inplace=True)
这是我为每个用户每月调用的 DataFrame 代码:
#For each user,find the number of calls made and minutes used per month:
calls_mins_per_month = megaline_calls.groupby([''user_id'',"call_month"]).agg({"call_id": len,"duration": "sum"})
calls_mins_per_month.rename(columns={''call_id'':''total_calls''},inplace=True)
calls_mins_per_month = calls_mins_per_month.reset_index()
#print(calls_mins_per_month[''duration''].isna().count())
calls_mins_per_month.fillna({''duration'':0},inplace=True)
有人能指出我做错了什么吗?
解决方法
您可以尝试一些操作:
使用 df.replace('' '','''') 可能是一个空白空间,并且不能将 fillna() 识别为 ''Nan'' 值 或检查 Nan 值实际上不是 ''Nan'' 字符串,如果是,那么您可以执行 df.replace(''Nan'','''')
希望你能解决
Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod
It's important to understand the Python execution model and precisely when function deFinitions and other important events occur when a module is imported or executed. Here, we show execution of our Python module as it's imported in a graphical debugging environment. We step through the top‑level statements in the module. What's important to realize here is that the def used for the fetch_words function isn't merely a declaration. It's actually a statement, which when executed in sequence with the other top‑level model scope code, causes the code within the function to be bound to the name of the function. When modules are imported or run, all of the top‑level statements are run, and this is the means by which the function within the module namespace are defined. We are sometimes asked about the difference between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module. But as we've seen, modules can be written for convenient import, convenient execution, or using the if dunder name = dunder main idiom, both. We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from within the REPL. Likewise, even modules, which are only ever meant to be imported in production settings, benefit from having executable test code. For this reason, nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution. Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool, in the vein of Windows batch files or UNIX Shell scripts, as many large and complex applications are built exclusively with python.
- def不仅仅是一个declaration声明,更是一条statement语句。它将其中的python代码于函数名绑定在一起
- 一个py文件就是一个模块,这个模块包含类或函数。你写python,要尽量将代码包装成函数和类,方便各种import
- 一个py文件也可看作是一个脚本,在系统命令行中运行
- python不仅仅是脚本语言,很多大型程序都是用python构建的
df.fillna() 不会替换所有 NaN 值
如何解决df.fillna() 不会替换所有 NaN 值
我正在尝试使用以下方法替换我的数据框中的 NaN 值:
values = {''pitch_type'': ''UN'',''px'': -1,''pz'': -1,''pitch_type_prev'': -1,''px_prev'': -1,''pz_prev'': -1}
df_sample.replace(np.nan,values)
它填充了除两个值之外的所有值。返回的输出是:
pitch_type px pz pitch_type_prev px_prev pz_prev
UN -1.000 -1.000 -1 NaN -1.000
FF 0.416 2.963 -1 NaN -1.000
FF -0.191 2.347 FF (0.0712,0.508] 2.963
有什么解决办法吗?
解决方法
您可以使用fillna
:
df_sample.fillna(values)
输出:
pitch_type px pz pitch_type_prev px_prev pz_prev
0 UN -1.000 -1.000 -1 -1 -1.000
1 FF 0.416 2.963 -1 -1 -1.000
2 FF -0.191 2.347 FF (0.0712,0.508] 2.963
今天的关于python – 在datetime对象上的pandas fillna和python里datetime怎么用的分享已经结束,谢谢您的关注,如果想了解更多关于.fillna 打破 .dt.normalize()、.reset_index 之后的 fillna() 用于创建 DataFrame、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、df.fillna() 不会替换所有 NaN 值的相关知识,请在本站进行查询。
本文标签: