簡體   English   中英

熊貓數據框中R的等效'rep'

[英]Equivalent 'rep' of R in Pandas dataframe

我已經搜索了一些類似的問題,例如“ Python中的等效R函數rep”。

在R中,rep可用於數組或數據框,並且可以將參數設置為each以指定是要重復每個元素還是要重復整個列表/數據框。

但是在Python中,您必須區分數組和數據框。

用於陣列, np.repeat將重復每個元素和np.tile重復整個陣列。

x=['a','b']

np.repeat(x,2)#repeat each element twice
Out[85]: array(['a', 'a', 'b', 'b'], dtype='<U1')

np.tile(x,2)#repeat the whole array twice
Out[86]: array(['a', 'b', 'a', 'b'], dtype='<U1')

對於熊貓數據框。 pd.concat可用於重復整個數據幀:

d=pd.DataFrame({'x':['a','b'],'y':['c','d']})
d
Out[94]: 
   x  y
0  a  c
1  b  d


pd.concat([d]*2)
Out[93]: 
   x  y
0  a  c
1  b  d
0  a  c
1  b  d

我的問題是如何重復熊貓數據框中的每一行,而不是整個重復。 我想要的結果是:

x y
a c
a c
b d 
b d

無論如何,我希望Python中有一個像“ rep”這樣的函數,它既可以用於list和dataframe,也可以指定整個重復或每個元素重復。

pandas您可以將reindexnp.repeat一起np.repeat

d.reindex(np.repeat(df.index.values,2))
   x  y
0  a  c
0  a  c
1  b  d
1  b  d

或重建您的數據框

pd.DataFrame(np.repeat(d.values,2,axis=0),columns=d.columns)
   x  y
0  a  c
1  a  c
2  b  d
3  b  d

concat wih sort_index

pd.concat([d]*2).sort_index()
   x  y
0  a  c
0  a  c
1  b  d
1  b  d

您還可以將np.repeatnp.arange 一起使用:

In [183]: d.iloc[np.repeat(np.arange(len(d)), 2)]
Out[183]: 
   x  y
0  a  c
0  a  c
1  b  d
1  b  d

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM