- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我得到了每月时间跟踪预订的数据。由于没有周末和国定假日的预订,因此数据缺失了几天。现在我正在扩展数据,以便它包括该月的所有日期,但只包含空数据。这就是我解决它的方法。我想知道是否有更优雅的方法来实现同样的事情?
为 start
列编制索引的示例输入数据:
+---------------------+--------+-------+----------+-----------------+---------+
| start | from | to | paused | hours_working | error |
|---------------------+--------+-------+----------+-----------------+---------|
| 2019-11-04 00:00:00 | 08:30 | 18:00 | 00:30 | 9 | 0 |
| 2019-11-05 00:00:00 | 09:30 | 19:00 | 00:30 | 9 | 0 |
| 2019-11-06 00:00:00 | 09:00 | 18:00 | 01:00 | 8 | 0 |
+---------------------+--------+-------+----------+-----------------+---------+
然后我检索第一个时间戳,然后用它来创建整个月的期间/索引:
# get first day of the month
first_timestamp = df.index[0].replace(day=1).strftime("%Y-%m-%d")
# build an index containing all days of that month
index_month = pd.date_range(start=first_timestamp, periods=pd.Period(first_timestamp).days_in_month, freq="24H")
index_month
导致:
DatetimeIndex(['2019-11-01', '2019-11-02', '2019-11-03', '2019-11-04',
'2019-11-05', '2019-11-06', '2019-11-07', '2019-11-08',
'2019-11-09', '2019-11-10', '2019-11-11', '2019-11-12',
'2019-11-13', '2019-11-14', '2019-11-15', '2019-11-16',
'2019-11-17', '2019-11-18', '2019-11-19', '2019-11-20',
'2019-11-21', '2019-11-22', '2019-11-23', '2019-11-24',
'2019-11-25', '2019-11-26', '2019-11-27', '2019-11-28',
'2019-11-29', '2019-11-30'],
dtype='datetime64[ns]', freq='24H')
不幸的是,我不知道如何将索引(日期范围)与原始数据连接起来。因此,我必须使用新索引创建一个新的空数据框,并将该数据框与原始数据连接起来。
df_index = pd.DataFrame(index_month, columns=['start'])
df_index.set_index('start', inplace=True)
df_month = df_index.join(df).reset_index()
from tabulate import tabulate
print(tabulate(df_month, headers='keys', tablefmt='psql'))
给我最终结果:
+----+---------------------+--------+-------+----------+-----------------+---------+
| | start | from | to | paused | hours_working | error |
|----+---------------------+--------+-------+----------+-----------------+---------|
| 0 | 2019-11-01 00:00:00 | nan | nan | nan | nan | nan |
| 1 | 2019-11-02 00:00:00 | nan | nan | nan | nan | nan |
| 2 | 2019-11-03 00:00:00 | nan | nan | nan | nan | nan |
| 3 | 2019-11-04 00:00:00 | 08:30 | 18:00 | 00:30 | 9 | 0 |
| 4 | 2019-11-05 00:00:00 | 09:30 | 19:00 | 00:30 | 9 | 0 |
| 5 | 2019-11-06 00:00:00 | 09:00 | 18:00 | 01:00 | 8 | 0 |
| 6 | 2019-11-07 00:00:00 | nan | nan | nan | nan | nan |
| 7 | 2019-11-08 00:00:00 | nan | nan | nan | nan | nan |
| 8 | 2019-11-09 00:00:00 | nan | nan | nan | nan | nan |
| 9 | 2019-11-10 00:00:00 | nan | nan | nan | nan | nan |
| 10 | 2019-11-11 00:00:00 | nan | nan | nan | nan | nan |
| 11 | 2019-11-12 00:00:00 | nan | nan | nan | nan | nan |
| 12 | 2019-11-13 00:00:00 | nan | nan | nan | nan | nan |
| 13 | 2019-11-14 00:00:00 | nan | nan | nan | nan | nan |
| 14 | 2019-11-15 00:00:00 | nan | nan | nan | nan | nan |
| 15 | 2019-11-16 00:00:00 | nan | nan | nan | nan | nan |
| 16 | 2019-11-17 00:00:00 | nan | nan | nan | nan | nan |
| 17 | 2019-11-18 00:00:00 | nan | nan | nan | nan | nan |
| 18 | 2019-11-19 00:00:00 | nan | nan | nan | nan | nan |
| 19 | 2019-11-20 00:00:00 | nan | nan | nan | nan | nan |
| 20 | 2019-11-21 00:00:00 | nan | nan | nan | nan | nan |
| 21 | 2019-11-22 00:00:00 | nan | nan | nan | nan | nan |
| 22 | 2019-11-23 00:00:00 | nan | nan | nan | nan | nan |
| 23 | 2019-11-24 00:00:00 | nan | nan | nan | nan | nan |
| 24 | 2019-11-25 00:00:00 | nan | nan | nan | nan | nan |
| 25 | 2019-11-26 00:00:00 | nan | nan | nan | nan | nan |
| 26 | 2019-11-27 00:00:00 | nan | nan | nan | nan | nan |
| 27 | 2019-11-28 00:00:00 | nan | nan | nan | nan | nan |
| 28 | 2019-11-29 00:00:00 | nan | nan | nan | nan | nan |
| 29 | 2019-11-30 00:00:00 | nan | nan | nan | nan | nan |
+----+---------------------+--------+-------+----------+-----------------+---------+
那么,这有什么问题呢?没什么,结果还好。但是我想知道是否有更好的方法来为那个月创建索引,然后如何将索引与原始数据连接起来?有什么建议么?只想学习并变得更好;)
最佳答案
如果 DatetimeIndex
的所有值都是唯一的,这里可以使用 DataFrame.reindex
- 对于新的 DatetimeIndex
的开始和结束,首先将第一个值转换为月份 Period,然后使用 Period.to_timestamp
:
first_per = df.index[0].to_period('m')
# build an index containing all days of that month
index_month = pd.date_range(start=first_per.to_timestamp(how='start'),
end=first_per.to_timestamp(how='end'),
freq="24H",
name='start')
df = df.reindex(index_month).reset_index()
print (df.head(10))
start from to paused hours_working error
0 2019-11-01 NaN NaN NaN NaN NaN
1 2019-11-02 NaN NaN NaN NaN NaN
2 2019-11-03 NaN NaN NaN NaN NaN
3 2019-11-04 08:30 18:00 00:30 9.0 0.0
4 2019-11-05 09:30 19:00 00:30 9.0 0.0
5 2019-11-06 09:00 18:00 01:00 8.0 0.0
6 2019-11-07 NaN NaN NaN NaN NaN
7 2019-11-08 NaN NaN NaN NaN NaN
8 2019-11-09 NaN NaN NaN NaN NaN
9 2019-11-10 NaN NaN NaN NaN NaN
关于python - Pandas 数据帧 : expanding data to full month,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247735/
pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能。有什么不同吗? 最佳答案 pivot_table没有 normalize争论,不幸的是。 在 crosstab
我能找到的最接近的答案似乎太复杂:How I can create an interval column in pandas? 如果我有一个如下所示的 pandas 数据框: +-------+ |
这是我用来将某一行的一列值移动到同一行的另一列的当前代码: #Move 2014/15 column ValB to column ValA df.loc[(df.Survey_year == 201
我有一个以下格式的 Pandas 数据框: df = pd.DataFrame({'a' : [0,1,2,3,4,5,6], 'b' : [-0.5, 0.0, 1.0, 1.2, 1.4,
所以我有这两个数据框,我想得到一个新的数据框,它由两个数据框的行的克罗内克积组成。正确的做法是什么? 举个例子:数据框1 c1 c2 0 10 100 1 11 110 2 12
TL;DR:在 pandas 中,如何绘制条形图以使其 x 轴刻度标签看起来像折线图? 我制作了一个间隔均匀的时间序列(每天一个项目),并且可以像这样很好地绘制它: intensity[350:450
我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1): Time1 Time2
从这个 df 去的正确方法是什么: >>> df=pd.DataFrame({'a':['jeff','bob','jill'], 'b':['bob','jeff','mike']}) >>> df
我想按周从 Pandas 框架中的列中累积计算唯一值。例如,假设我有这样的数据: df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,
数据透视表的表示形式看起来不像我在寻找的东西,更具体地说,结果行的顺序。 我不知道如何以正确的方式进行更改。 df示例: test_df = pd.DataFrame({'name':['name_1
我有一个数据框,如下所示。 Category Actual Predicted 1 1 1 1 0
我有一个 df,如下所示。 df: ID open_date limit 1 2020-06-03 100 1 2020-06-23 500
我有一个 df ,其中包含与唯一值关联的各种字符串。对于这些唯一值,我想删除不等于单独列表的行,最后一行除外。 下面使用 Label 中的各种字符串值与 Item 相关联.所以对于每个唯一的 Item
考虑以下具有相同名称的列的数据框(显然,这确实发生了,目前我有一个像这样的数据集!:() >>> df = pd.DataFrame({"a":range(10,15),"b":range(5,10)
我在 Pandas 中有一个 DF,它看起来像: Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2
如何减去两列之间的时间并将其转换为分钟 Date Time Ordered Time Delivered 0 1/11/19 9:25:00 am 10:58:00 am
我试图理解 pandas 中的下/上百分位数计算,但有点困惑。这是它的示例代码和输出。 test = pd.Series([7, 15, 36, 39, 40, 41]) test.describe(
我有一个多索引数据框,如下所示: TQ bought HT Detailed Instru
我需要从包含值“低”,“中”或“高”的数据框列创建直方图。当我尝试执行通常的df.column.hist()时,出现以下错误。 ex3.Severity.value_counts() Out[85]:
我试图根据另一列的长度对一列进行子串,但结果集是 NaN .我究竟做错了什么? import pandas as pd df = pd.DataFrame([['abcdefghi','xyz'],
我是一名优秀的程序员,十分优秀!