是否可以将多个DataFrame混合在一起?
例如,我有一个DataFrame df1和一个DataFrame df2.我想随机地对行进行随机播放,但对于两个DataFrame以相同的方式.
例
DF1:
|___|_______|
| 1 | ... |
| 2 | ... |
| 3 | ... |
| 4 | ... |
DF2:
|___|_______|
| 1 | ... |
| 2 | ... |
| 3 | ... |
| 4 | ... |
在洗牌之后,两个DataFrame的可能订单可能是:
|___|_______|
| 2 | ... |
| 3 | ... |
| 4 | ... |
| 1 | ... |
解决方法:
我认为你可以通过将numpy.random.permutation
应用于索引来加倍reindex
,但是必须两个DataFrame具有相同的长度和相同的唯一索引值:
df1 = pd.DataFrame({'a':range(5)})
print (df1)
a
0 0
1 1
2 2
3 3
4 4
df2 = pd.DataFrame({'a':range(5)})
print (df2)
a
0 0
1 1
2 2
3 3
4 4
idx = np.random.permutation(df1.index)
print (df1.reindex(idx))
a
2 2
4 4
1 1
3 3
0 0
print (df2.reindex(idx))
a
2 2
4 4
1 1
3 3
0 0
替代reindex_axis
:
print (df1.reindex_axis(idx, axis=0))
print (df2.reindex_axis(idx, axis=0))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。