【问题标题】:Random order of one pandas.DataFrame with respect of other一个 pandas.DataFrame 相对于其他的随机顺序
【发布时间】:2020-10-06 12:17:51
【问题描述】:

我有以下结构:

data_Cnx = pd.read_csv(path_Connection,sep='\t',header=None)
data_Cnx.columns = ["ConnectionID"]
data_Srv = pd.read_csv(path_Service,sep='\t',header=None)
data_Srv.columns = ["ServiceID"]

可视化如下:

print(data_Cnx)
      ConnectionID
    0   CN0120
    1   CN0121
    2   CN0122
    3   CN0123
    4   CN0124
    ...           ...
    20   CN0166
    21   CN0167
    22   CN0168
    23   CN0171
    24   CN0172
    [25 rows x 1 columns]      
  
print(data_Srv)
       ServiceID
    0   ST030
    1   ST030
    2   ST030
    3   ST030
    4   ST030
    ...          ...
    20  ST040
    21  ST040
    22  ST040
    23  ST050
    24  ST050
    [25 rows x 1 columns]

从字面上看,data_Cnx 中的每个元素对应于data_Srv 中的一个并行元素,遵守顺序。例如:

CN0120 corresponds to ST030
CN0121 corresponds to ST030
....
CN0166 corresponds to ST040
CN0167 corresponds to ST040
...
CN0171 corresponds to ST050
...

我想要另一种结构或不同的data_Cnxdata_Srv,其中data_Cnx 的顺序可以随机化,但始终与data_Srv 中对应的内容有关。例如:

data_Cnxdata_Srv 可视化如下:

print(data_Cnx)
      ConnectionID
    0    CN0120
    1    CN0168
    2    CN0156
    3    CN0133
    4    CN0161
    ...           ...
    20   CN0121
    21   CN0143
    22   CN0127
    23   CN0151
    24   CN0132
print(data_Srv)    
    [25 rows x 1 columns]        ServiceID
    0   ST030
    1   ST040
    2   ST070
    3   ST010
    4   ST040
    ...          ...
    20  ST030
    21  ST050
    22  ST030
    23  ST070
    24  ST010
    

我正在考虑使用randn,但显然它使用整数作为参数。您对如何实现这一点有更简单的想法吗?

【问题讨论】:

  • 问题是什么?

标签: python pandas random


【解决方案1】:

我只是找到了以下作品:

bigdata = pd.concat([data_Srv,data_Cnx], axis=1)
bigdata.sample(n = 20)

如果有人提出更好的想法,我会打开尝试:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2014-04-29
    • 2020-09-19
    • 1970-01-01
    • 2012-12-02
    • 2014-04-08
    相关资源
    最近更新 更多