python - 重复行以获取所有星期的数据

标签 python pandas

我想知道是否有一种不太笨拙的方法来为以下示例创建所有星期的所有组合:

df = pd.DataFrame({
    'Gender': ['female', 'female', 'female', 'female'],
    'Bla': ['a', 'a', 'a', 'a'],
    'Week': [1, 2, 3, 4]
})
print(df)

number_of_weeks = 52
temp = df[['Gender', 'Bla']].drop_duplicates()
results = temp.loc[temp.index.repeat(number_of_weeks)]
results["Week"] = range(1, len(results) + 1, 1)
print(results)

这种工作,但它也应该适用于分类变量超过 1 个“级别”的情况。

df = pd.DataFrame({
    'Gender': ['female', 'female', 'female', 'female'],
    'Bla': ['a', 'b', 'a', 'a'],
    'Week': [1, 2, 3, 4]
})

上面的代码会将其扩展到不可行的周数。

最佳答案

尝试使用concat:

temp = df[["Gender", "Bla"]].drop_duplicates()
output = pd.concat([temp]*52, ignore_index=True).assign(week=list(range(1,53))*temp.shape[0])

>>> output
     Gender Bla  week
0    female   a     1
1    female   b     2
2    female   a     3
3    female   b     4
4    female   a     5
..      ...  ..   ...
99   female   b    48
100  female   a    49
101  female   b    50
102  female   a    51
103  female   b    52

[104 rows x 3 columns]

关于python - 重复行以获取所有星期的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71130439/

相关文章:

python - 如何判断哪个 Keras 模型更好?

python - pandas时间序列数据预处理

python - Pandas 用两个不同的 ID 压平数据框?

python - 为什么更新 sys.path 不会影响 python 将搜索的目录列表

python - 高效地与请求异步下载文件

python - Python 数据框中的插值

python - 根据 NaN 将列值替换为 0 或 1

python - 合乎逻辑或在 Pandas 面具 list 上

python - 类型错误 : unhashable type: 'list' when dynamically creating 'variables'

python - 来自 Geopandas GeoDataFrame 的 Choropleth map