简体   繁体   English

将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值

[英]Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column

I have a df like this:我有一个这样的df:

    MEMBER_ID FirstName LastName  I    MONTH
0   1          John      Doe      10   0
1   2          Mary      Jones    15   0
2   3          Andy      Right    8    0

I need to create a new df (df_new) which contains each row corresponding to a unique MEMBER_ID, replicated by the amount of times that is in the 'I' column, and the 'MONTH' column has to be filled from 0 and up to and including the value of 'I' in the original df.我需要创建一个新的 df (df_new),其中包含对应于唯一 MEMBER_ID 的每一行,按“I”列中的次数复制,并且“MONTH”列必须从 0 到最多填充并在原始 df 中包含“I”的值。 For example: first row (MEMBER_ID==1) has to be replicated 10 times (value of 'I') and the only difference would be the 'MONTH' column which will go from 0 to 10. After that the rows continue for the next unique value in the 'MEMBER_ID' column.例如:第一行 (MEMBER_ID==1) 必须被复制 10 次('I' 的值),唯一的区别是 'MONTH' 列,它将 go 从 0 到 10。之后行继续'MEMBER_ID' 列中的下一个唯一值。 So I need the df_new to look like this:所以我需要 df_new 看起来像这样:

    MEMBER_ID FirstName LastName  I    MONTH
0   1          John      Doe      10   0
1   1          John      Doe      10   1
2   1          John      Doe      10   2
3   1          John      Doe      10   3
...
10  1          John      Doe      10   10
11  2          Mary      Jones    15   0
12  2          Mary      Jones    15   1
13  2          Mary      Jones    15   2
...
N-1 3          Andy      Right    8    7
N   3          Andy      Right    8    8 

I have tried this but it gives me gibberish:我试过这个,但它给了我胡言乱语:

df_new=pd.DataFrame(columns=['MEMBER_ID','FirstName','LastName','I','MONTH'])

for i in range(len(df)):
   max_i=df.iloc[i]["I"]  #this gets the value in the "I" column
   for j in range(0,max_i+1): #to append same row max_i+1 times since I need MONTH to start with 0
      df_new.loc[i]=df.iloc[i]  #this picks the whole row from the original df
      df_new["MONTH"]=j      #this assigns the value of each iteration to the MONTH column
      df_new=df_new.append(df_new.loc[i],ignore_index=True)

Thank you for your help, dear community!感谢您的帮助,亲爱的社区!

I was able to fix the SettingWithCopyWarning with this:我能够通过以下方式修复 SettingWithCopyWarning:

index =0
for i in range(len(df)):
    for j in range(df.iloc[i]["I"]+1):
        row=df.iloc[i]
        df_new=df_new.append(row,ignore_index=True)
        df_new.at[index,'MONTH']=j
        index+=1

df.head()

The problem is, that you overwrite df_new many times.问题是,您多次覆盖 df_new 。 This should work.这应该有效。 df ist the old DataFrame df是旧的 DataFrame

df_new = pd.DataFrame()

for member in range(len(df)): #iterate over every member
    for count in range(df.iloc[member]['I']+1): # you want to add 'I'+1 rows
        row = df.iloc[member] # select the row you want to add
        row['MONTH'] = count #change the month-vale of the row to add
        df_new = df_new.append(row,ignore_index=True) # add the row to the new DataFrame
df_new

Otherwise please show, what's wrong with the output.否则请显示,output 有什么问题。

暂无
暂无

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

相关问题 在Pandas数据框中一次更改一行 - Altering one row at a time in a Pandas dataframe 熊猫:在数据框的最后一行添加一个具有单个值的新列 - Pandas: add a new column with one single value at the last row of a dataframe 有没有办法向pandas数据框添加新列,将新列的每个唯一值附加到数据帧的每个现有行? - Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe? Pandas,如何将一行中的值与同一列中的所有其他行进行比较,并将其作为新列中的新行值添加? - Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column? Python Pandas - 在特定行上添加列,将特定行从一个 dataframe 添加到另一个 - Python Pandas - add column on a specific row, add specific row from one dataframe to another 如何多次在pandas数据帧中添加特定行 - How to add a specific row in a pandas dataframe multiple times 将相同的列表添加到新列中的pandas DataFrame中的每一行 - Adding the same list to each row in a pandas DataFrame in a new column 如何从熊猫数据框创建汇总新行并将其添加回仅特定列的同一数据框 - How to create a summarize new row from a pandas Dataframe and add it back to the same Dataframe for only specific columns 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)? 在 pandas 中,如何将新行插入 dataframe 一次一列值 - In pandas, how do I insert a new row into a dataframe one column value at a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM