首页 > 解决方案 > 在熊猫数据框中,如何在不使用循环的情况下根据一列的数据设置其他列的值?

问题描述

我正在尝试构建一个用于线性回归的数据框。我想包括 11 个独立的“虚拟”变量,这些变量根据一年中的月份设置为 1 或 0。在不离题太远的情况下,我使用 11 个变量而不是 12 个变量,因为第 12 个月被截距捕获。

我知道使用 pandas 可以完成很多事情,而无需遍历整个数据帧,并且以这种方式做事通常比使用循环更快。

那么,是否可以从我的日期列中获取月份,并根据该月份将单独的列动态设置为 1 或 0?还是我在问一个愚蠢的问题?

编辑:我应该包含更多信息。数据框的结构如下:

日期 库存单位 订购单位 会话 兑换率
2020/01/30 abc123 20 200 0.1
2020/01/31 abc123 10 100 0.1
2020/02/01 abc123 15 60 0.25

我想让它看起来像这样:

日期 库存单位 订购单位 会话 兑换率 一月 二月
2020/01/30 abc123 20 200 0.1 1 0
2020/01/31 abc123 10 100 0.1 1 0
2020/02/01 abc123 15 60 0.25 0 1

我目前用来完成此操作的代码是:

x = 1
while x < 12:
    month = calendar.month_name[x]
    df[month] = 0
    x += 1

for index, row in df.iterrows():
    d = row[0]
    month = d.strftime("%B")
    if not month == "December":
        df.at[index, month] = 1

    df.fillna(0, inplace=True)

只是不确定这是否是实现这一目标的最佳方式。

标签: pythonpandasdataframenumpydatetime

解决方案


我的方法是首先使用dt.month从每个月获取月份数:

df['Date'].dt.month
0    1
1    1
2    2
Name: Date, dtype: int64

然后使用带有索引的交叉表来获取计数列表:

pd.crosstab(
        df.index,
        df['Date'].dt.month
    )
Date   1  2
row_0      
0      1  0
1      1  0
2      0  1

然后合并回索引上的 DF:

df = (
    df.merge(pd.crosstab(
        df.index,
        df['Date'].dt.month
    ),
        left_index=True,
        right_index=True)
)

输出:

        Date     sku  units ordered  sessions  conversion rate  1  2
0 2020-01-30  abc123             20       200             0.10  1  0
1 2020-01-31  abc123             10       100             0.10  1  0
2 2020-02-01  abc123             15        60             0.25  0  1

最后,使用由日历 api生成的映射器重命名列:

df = df.rename(columns={month_num: calendar.month_name[month_num]
                        for month_num in range(1, 13)})

全部一起:

import pandas as pd
import calendar

df = pd.DataFrame(
    {'Date': {0: '2020/01/30', 1: '2020/01/31', 2: '2020/02/01'},
     'sku': {0: 'abc123', 1: 'abc123', 2: 'abc123'},
     'units ordered': {0: 20, 1: 10, 2: 15},
     'sessions': {0: 200, 1: 100, 2: 60},
     'conversion rate': {0: 0.1, 1: 0.1, 2: 0.25}})
df['Date'] = df['Date'].astype('datetime64[ns]')

df = (
    df.merge(pd.crosstab(
        df.index,
        df['Date'].dt.month
    ),
        left_index=True,
        right_index=True)
)

df = df.rename(columns={month_num: calendar.month_name[month_num]
                        for month_num in range(1, 13)})

print(df.to_string())

输出:

        Date     sku  units ordered  sessions  conversion rate  January  February
0 2020-01-30  abc123             20       200             0.10        1         0
1 2020-01-31  abc123             10       100             0.10        1         0
2 2020-02-01  abc123             15        60             0.25        0         1

推荐阅读