pandas 将元组复制到 Dataframe 中的整行

7xzttuei  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(188)

我正在尝试对 Dataframe 执行for循环,并将整行复制到临时 Dataframe 。行的类型是元组,那么我如何将元组中的每一项复制到 Dataframe 中同一行的不同列中?请参见我的 Dataframe 示例和我所需的输出
下面是我的 Dataframe df:

╔════════╦════════════════╦═════════════════════╦═══════════════════════╦═════════════╗
║   ID   ║      Name      ║       Address       ║         Email         ║    Items    ║
╠════════╬════════════════╬═════════════════════╬═══════════════════════╬═════════════╣
║ 839843 ║ John Smith     ║ 55 Apple Lane       ║ jsmith@gmail.com      ║ 827,937,392 ║
║ 327569 ║ Tom Hanks      ║ 16 Cauliflower Road ║ thanks@gmail.com      ║ 947,540     ║
║ 924852 ║ Alison Johnson ║ 85 Main Street      ║ Ajohnson@yahoo.com    ║ 838         ║
║ 949325 ║ Frank Rizzo    ║ 218 Orange Road     ║ frizzo313@hotmail.com ║ 494,386,285 ║
║ 373202 ║ Kelly Chang    ║ 19 First Avenue     ║ kchang@gmail.com      ║ 928,502,214 ║
║ 928436 ║ Lisa Thomas    ║ 95 Albany Way       ║ lthomas@hotmail.com   ║ 455,953     ║
╚════════╩════════════════╩═════════════════════╩═══════════════════════╩═════════════╝

这是我想要的输出:

╔════════╦══════╦════════════════╦═════════════════════╦═══════════════════════╗
║   ID   ║ Item ║      Name      ║       Address       ║         Email         ║
╠════════╬══════╬════════════════╬═════════════════════╬═══════════════════════╣
║ 839843 ║  827 ║ John Smith     ║ 55 Apple Lane       ║ jsmith@gmail.com      ║
║ 839843 ║  937 ║ John Smith     ║ 55 Apple Lane       ║ jsmith@gmail.com      ║
║ 839843 ║  392 ║ John Smith     ║ 55 Apple Lane       ║ jsmith@gmail.com      ║
║ 327569 ║  947 ║ Tom Hanks      ║ 16 Cauliflower Road ║ thanks@gmail.com      ║
║ 327569 ║  540 ║ Tom Hanks      ║ 16 Cauliflower Road ║ thanks@gmail.com      ║
║ 924852 ║  838 ║ Alison Johnson ║ 85 Main Street      ║ Ajohnson@yahoo.com    ║
║ 949325 ║  494 ║ Frank Rizzo    ║ 218 Orange Road     ║ frizzo313@hotmail.com ║
║ 949325 ║  386 ║ Frank Rizzo    ║ 218 Orange Road     ║ frizzo313@hotmail.com ║
║ 949325 ║  285 ║ Frank Rizzo    ║ 218 Orange Road     ║ frizzo313@hotmail.com ║
║ 373202 ║  928 ║ Kelly Chang    ║ 19 First Avenue     ║ kchang@gmail.com      ║
║ 373202 ║  502 ║ Kelly Chang    ║ 19 First Avenue     ║ kchang@gmail.com      ║
║ 373202 ║  215 ║ Kelly Chang    ║ 19 First Avenue     ║ kchang@gmail.com      ║
║ 928436 ║  455 ║ Lisa Thomas    ║ 95 Albany Way       ║ lthomas@hotmail.com   ║
║ 928436 ║  953 ║ Lisa Thomas    ║ 95 Albany Way       ║ lthomas@hotmail.com   ║
╚════════╩══════╩════════════════╩═════════════════════╩═══════════════════════╝

这是我所要解决的问题但至今没有成功的代码:

temp_df = df.iloc[:0] 

c=0
i=0
for row in df.iterrows():
    items = row[1][4].split(',') 
    for item in items:            
        temp_df.at[i+c,:] = row
        temp_df.at[-1, 'Items'] = item
        c=c+1
    i=i+c
    c=0

希望有人能帮上忙。

uidvcgyl

uidvcgyl1#

假设 your_file.csv 是示例中提到的第一个表,您可以将其读入df并执行以下操作:

解决方案1:

解析它的行以获取temp_df中所需的内容:

temp_df_records = []

for _, row in df.iterrows():
    items = row["Items"].split(',')
    row_dict = row.drop("Items").to_dict()
    for item in items:
        row_dict["Item"] = item
        temp_df_records.append(row_dict.copy())

temp_df = pd.DataFrame.from_records(temp_df_records)

解决方案2:

您可以将列“Items”的值转换为列表,并使用.explode()方法构造temp_df

temp_df = df.copy().rename(columns={"Items": "Item"})
temp_df["Item"] = temp_df["Item"].apply(lambda items: items.split(","))
temp_df = temp_df.explode("Item").reset_index(drop=True)

两种解决方案中的输出(temp_df):

ID            Name              Address                  Email Item
0   839843      John Smith        55 Apple Lane       jsmith@gmail.com  827
1   839843      John Smith        55 Apple Lane       jsmith@gmail.com  937
2   839843      John Smith        55 Apple Lane       jsmith@gmail.com  392
3   327569       Tom Hanks  16 Cauliflower Road       thanks@gmail.com  947
4   327569       Tom Hanks  16 Cauliflower Road       thanks@gmail.com  540
5   924852  Alison Johnson       85 Main Street     Ajohnson@yahoo.com  838
6   949325     Frank Rizzo      218 Orange Road  frizzo313@hotmail.com  494
7   949325     Frank Rizzo      218 Orange Road  frizzo313@hotmail.com  386
8   949325     Frank Rizzo      218 Orange Road  frizzo313@hotmail.com  285
9   373202     Kelly Chang      19 First Avenue       kchang@gmail.com  928
10  373202     Kelly Chang      19 First Avenue       kchang@gmail.com  502
11  373202     Kelly Chang      19 First Avenue       kchang@gmail.com  214
12  928436     Lisa Thomas        95 Albany Way    lthomas@hotmail.com  455
13  928436     Lisa Thomas        95 Albany Way    lthomas@hotmail.com  953

相关问题