我正在尝试对 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
希望有人能帮上忙。
1条答案
按热度按时间uidvcgyl1#
假设 your_file.csv 是示例中提到的第一个表,您可以将其读入
df
并执行以下操作:解决方案1:
解析它的行以获取
temp_df
中所需的内容:解决方案2:
您可以将列“Items”的值转换为列表,并使用
.explode()
方法构造temp_df
:两种解决方案中的输出(
temp_df
):