简体   繁体   English

如何在pandas DataFrame中复制行并添加id列

[英]How do I copy rows in a pandas DataFrame and add an id column

I have a dataframe such as: 我有一个数据框,如:

from pandas import DataFrame
import pandas as pd
x = DataFrame.from_dict({'farm' : ['A','B','A','B'], 
                         'fruit':['apple','apple','pear','pear']})

How can I copy it N times with an id, eg. 如何使用id复制N次,例如。 to output (for N=2 ): 输出(对于N=2 ):

  farm  fruit  sim
0    A  apple    0
1    B  apple    0
2    A   pear    0
3    B   pear    0
0    A  apple    1
1    B  apple    1
2    A   pear    1
3    B   pear    1

I tried an approach which works on dataframes in R: 我尝试了一种适用于R中数据帧的方法:

from numpy import arange
N = 2
sim_ids = DataFrame(arange(N))
pd.merge(left=x, right=sim_ids, how='left')

but this fails with the error MergeError: No common columns to perform merge on . 但是这会因错误MergeError: No common columns to perform merge on而失败MergeError: No common columns to perform merge on

Thanks. 谢谢。

Not sure what R is doing there, but here's a way to do what you want: 不确定R在那里做什么,但这是一种做你想做的事情的方法:

In [150]: x
Out[150]:
  farm  fruit
0    A  apple
1    B  apple
2    A   pear
3    B   pear

[4 rows x 2 columns]

In [151]: N = 2

In [152]: DataFrame(tile(x, (N, 1)), columns=x.columns).join(DataFrame({'sims': repeat(arange(N), len(x))}))
Out[152]:
  farm  fruit  sims
0    A  apple     0
1    B  apple     0
2    A   pear     0
3    B   pear     0
4    A  apple     1
5    B  apple     1
6    A   pear     1
7    B   pear     1

[8 rows x 3 columns]

In [153]: N = 3

In [154]: DataFrame(tile(x, (N, 1)), columns=x.columns).join(DataFrame({'sims': repeat(arange(N), len(x))}))
Out[154]:
   farm  fruit  sims
0     A  apple     0
1     B  apple     0
2     A   pear     0
3     B   pear     0
4     A  apple     1
5     B  apple     1
6     A   pear     1
7     B   pear     1
8     A  apple     2
9     B  apple     2
10    A   pear     2
11    B   pear     2

[12 rows x 3 columns]

I might do something like: 我可能会这样做:

>>> df_new = pd.concat([df]*2)
>>> df_new["id"] = df_new.groupby(level=0).cumcount()
>>> df_new
  farm  fruit  id
0    A  apple   0
1    B  apple   0
2    A   pear   0
3    B   pear   0
0    A  apple   1
1    B  apple   1
2    A   pear   1
3    B   pear   1

[8 rows x 3 columns]

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

相关问题 如何为 Pandas 数据框列中的每个唯一值添加重复的月份行? - How do I add repeated month rows for every unique value in a pandas dataframe column? 如何在 Pandas Dataframe 中获取行并转换为列的值? - How do I take rows in Pandas Dataframe and transform into values for a Column? 如何在 Pandas Dataframe 中增量添加行? - How do I incrementally add rows in Pandas Dataframe? 如何将PANDAS数据帧的一行添加到其余行? - How do I add one row of a PANDAS dataframe to the rest of the rows? 如何在pandas数据帧的第二行中添加列标题? - How do i add column header, in the second row in a pandas dataframe? 如何在 Pandas 数据框的列中添加空白单元格? - How do I add a blank cell inside a column of a Pandas dataframe? 在 pandas dataframe 中,如何根据列值过滤行,进行计算并将结果分配给新列? - In a pandas dataframe, how can I filter the rows based on a column value, do calculation and assign the result to a new column? 如何在每5行后在pandas DataFrame中添加新id, - How can I add the new id in the pandas DataFrame after every 5 rows, 给定来自另一列的条件,如何遍历特定 Pandas DataFrame 列的行? - How do I iterate over the rows of a specific Pandas DataFrame column, given a condition from another column? 如何按 Pandas DataFrame 中的各个行添加新列数据 - How to add new column data by individual rows in Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM