每个 id 创建 n 行 | pandas [英] Create n rows per id | Pandas
问题描述
我有一个数据框 df
如下:
I have a Dataframe df
as follows:
id | 吊球 | 地址 | addr2 |
---|---|---|---|
a1 | 001 | 1234 | 0 |
a1 | 001 | 1233 | 0 |
a3 | 003 | 1221 | 0 |
a4 | 009 | 1234 | 0 |
我想为每个 id 生成 n
(让我们取 4)行,其他列是 null/na/nan
值.所以,上表要转化为:
I want to generate n
(let's take 4) rows per id, with the other columns being null/na/nan
values.
So, the above table is to be transformed to:
id | 吊球 | 地址 | addr2 |
---|---|---|---|
a1 | 001 | 1234 | 0 |
a1 | 001 | 1233 | 0 |
a1 | 001 | 无 | 无 |
a1 | 无 | 无 | 无 |
a3 | 003 | 1221 | 0 |
a3 | 无 | 无 | 无 |
a3 | 无 | 无 | 无 |
a3 | 无 | 无 | 无 |
a4 | 009 | 1234 | 0 |
a4 | 无 | 无 | 无 |
a4 | 无 | 无 | 无 |
a4 | 无 | 无 | 无 |
我怎样才能做到这一点?在执行时,我将有 500-700 个 ID,而 n 始终为 70(因此每个 ID 应该有 70 行).
How can I achieve this? I will have anywhere from 500-700 ids at the time of execution and the n will always be 70 (so each id should have 70 rows).
我想创建一个循环来创建一行,按 id 进行分组,看看它是否小于 70 并重复该过程,但最终会执行很多不必要的操作.
I wanted to create a loop that would create a row, do a group by id, see if it's less than 70 and repeat the process but it would end up doing a lot of unnecessary operations.
推荐答案
这是一个使用 Counter 计算每个 ID 需要多少额外行,然后追加新数据的解决方案:
Here's a solution using Counter to count how many extra rows you need for each ID, and then just appending the new data:
from collections import Counter
id_count = Counter(df['id'])
# Create lists of each id repeated the number of times each is needed:
n = 4
id_values = [[i] * (n - id_count[i]) for i in id_count.keys()]
# Flatten to a single list:
id_values = [i for s in id_values for i in s]
# Create as new DataFrame and append to existing data:
new_data = pd.DataFrame({"id": id_values})
df = df.append(new_data).sort_values(by="id")
这篇关于每个 id 创建 n 行 | pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!