简体   繁体   English

熊猫根据列的每个唯一元素添加行

[英]Pandas add rows according for each unique element of a column

I've got a dataframe, like so: 我有一个数据框,像这样:

ID A 
0  z
2  z
2  y
5  x

To which I want to add rows for each unique value of an ID column: 我想为ID列的每个唯一值添加行:

ID A
0  z
2  z
2  y
5  x
0  b
2  b
5  b

I'm currently doing so in a very naïve way, which is quite inefficient/slow: 我目前正在以一种非常幼稚的方式执行此操作,这是非常低效/缓慢的:

IDs = df["ID"].unique()
for ID in IDs: 
    df = df.append(pd.DataFrame([[ID, "b"]], columns=df.columns), ignore_index=True)

How would I go to accomplish the same without the explicit foreach, only pandas function calls? 如果没有显式的foreach(仅熊猫函数调用),我将如何完成相同的任务?

Use drop_duplicates , rewrite column by assign and append or concat to original DataFrame : 使用drop_duplicates ,通过为原始DataFrame assignappendconcat来重写列:

df = df.append(df.drop_duplicates("ID").assign(A='B'), ignore_index=True)
#alternative
#df = pd.concat([df, df.drop_duplicates("ID").assign(A='B')], ignore_index=True)
print (df)
   ID  A
0   0  z
1   2  z
2   2  y
3   5  x
4   0  B
5   2  B
6   5  B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM