每个 id 创建 n 行 | pandas [英] Create n rows per id | Pandas

查看:34
本文介绍了每个 id 创建 n 行 | pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框 df 如下:

I have a Dataframe df as follows:

<头>
id吊球地址addr2
a100112340
a100112330
a300312210
a400912340

我想为每个 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
a100112340
a100112330
a1001
a1
a300312210
a3
a3
a3
a400912340
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆