[英]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
您可以將reindex
與np.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.