让它成为下面的巨 Python Pandas DataFrame,其中每一行代表一个人在wine 店的住宿.

| entry_date | exit_date  | days   | other_columns |
| ---------- | ---------- | ------ | ------------- |
| 2022-02-01 | 2022-02-05 | 5      |  ...          |
| 2022-02-02 | 2022-02-03 | 2      |  ...          |
| 2022-04-10 | 2022-04-13 | 4      |  ...          |
| 2022-04-11 | 2022-04-12 | 2      |  ...          |
| 2022-04-12 | 2022-04-13 | 2      |  ...          |
| 2022-11-10 | 2022-11-15 | 6      |  ...          |

我想从前面的DataFrame创建一个DataFrame,它表示每一天的wine 店当时的入住率.我没有考虑黑夜,只考虑了白天的变化.

| date       | ocupation  |
| ---------- | ---------- |
| 2022-02-01 |     1      |
| 2022-02-02 |     2      |
| 2022-02-03 |     2      |
| 2022-02-04 |     1      |
| 2022-02-05 |     1      |
| 2022-04-10 |     1      |
| 2022-04-11 |     2      |
| 2022-04-12 |     3      |
| 2022-04-13 |     2      |
| 2022-11-10 |     1      |
| 2022-11-11 |     1      |
| 2022-11-12 |     1      |
| 2022-11-13 |     1      |
| 2022-11-14 |     1      |
| 2022-11-15 |     1      |

推荐答案

使用:

#convert column to datetimes
df['entry_date'] = pd.to_datetime(df['entry_date'])

#repeat rows by days column
df = df.loc[df.index.repeat(df['days'])]

#create days timedeltas
td = pd.to_timedelta(df.groupby(level=0).cumcount(), unit='d')

#add timedeltas by datetiems and count to 2 columns DataFrame
df1 = (df['entry_date'].add(td)
                       .value_counts()
                       .sort_index()
                       .rename_axis('date')
                       .reset_index(name='ocupation'))
print (df1)

         date  ocupation
0  2022-02-01         1
1  2022-02-02         2
2  2022-02-03         2
3  2022-02-04         1
4  2022-02-05         1
5  2022-04-10         1
6  2022-04-11         2
7  2022-04-12         3
8  2022-04-13         2
9  2022-11-10         1
10 2022-11-11         1
11 2022-11-12         1
12 2022-11-13         1
13 2022-11-14         1
14 2022-11-15         1

Performance:样本数据重复Performance0次:

df = pd.concat([df] * 1000, ignore_index=True)

def jez(df):
    #convert column to datetimes
    df['entry_date'] = pd.to_datetime(df['entry_date'], dayfirst=True)
    
    #repeat rows by days column
    df = df.loc[df.index.repeat(df['days'])]
    
    #create days timedeltas
    td = pd.to_timedelta(df.groupby(level=0).cumcount(), unit='d')
    
    #add timedeltas by datetiems and count to 2 columns DataFrame
    return (df['entry_date'].add(td)
                           .value_counts()
                           .sort_index()
                           .rename_axis('date')
                           .reset_index(name='ocupation'))
    


def moz(df):
    return (pd.Series([d for start, end in zip(df['entry_date'], df['exit_date'])
            for d in pd.date_range(start, end, freq='D')], name='date')
   .value_counts(sort=False)
   .reset_index(name='ocupation')
 )

In [122]: %timeit jez(df)
15.3 ms ± 470 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [123]: %timeit moz(df)
2.31 s ± 140 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Python相关问答推荐

将整组数组拆分为最小值与最大值之和的子数组

在Python中处理大量CSV文件中的数据

如何使用matplotlib在Python中使用规范化数据和原始t测试值创建组合热图?

如何删除索引过go 的lexsort深度可能会影响性能?' &>

需要计算60,000个坐标之间的距离

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

使用miniconda创建环境的问题

如何标记Spacy中不包含特定符号的单词?

如何从具有不同len的列表字典中创建摘要表?

删除所有列值,但判断是否存在任何二元组

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

当独立的网络调用不应该互相阻塞时,'

DataFrames与NaN的条件乘法

如何在UserSerializer中添加显式字段?

如何启动下载并在不击中磁盘的情况下呈现响应?

Python全局变量递归得到不同的结果

寻找Regex模式返回与我当前函数类似的结果

如何使用OpenGL使球体遵循Python中的八样路径?

以异步方式填充Pandas 数据帧

如何使用Azure Function将xlsb转换为xlsx?