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")
import pandas as pd
def replication(n, table):
cols = [x in table.columns]
empty_tab = table.copy()
for x in cols:
if x != 'ID':
empty_tab[x] = np.nan
for x in range(n):
table = pd.concat([table, empty_tab.copy()], columns = cols)
return table
您可以枚举
id
中的行,然后尝试stack/unstack
或pivot
:下面是一个解决方案,使用计数器计算每个ID需要多少额外的行,然后添加新数据:
您可以使用Pandas中的concat函数来优化运行时间,代码可以如下所示:
在这种情况下,第二个副本()实际上不是必需的
相关问题 更多 >
编程相关推荐