在保持内部顺序的同时打乱 DataFrame
Shuffle a DataFrame while keeping internal order
我有一个包含预处理数据的数据框,这样每 4 行就是一个序列(稍后将被重塑并用于 lstm 训练)。
我想打乱数据帧,但我想保持每个行序列不变。例如:
a = [1,2,3,4,10,11,12,13,20,21,22,23]
会变成:a = [20,21,22,23,1,2,3,4,10,11,12,13]
.
df.sample(frac=1)
不够,因为它会破坏序列。
解决方案,感谢@Wen-Ben:
seq_length = 4
length_array = np.arange((df.shape[0]//seq_length)*seq_length)
trunc_data = df.head((df.shape[0]//seq_length)*seq_length)
d = {x : y for x, y in trunc_data.groupby(length_array//seq_length)}
yourdf = pd.concat([d.get(x) for x in np.random.choice(len(d),len(d.keys()),replace=False)])
这是你需要的吗,np.random.choice
d={x : y for x, y in df.groupby(np.arange(len(df))//4)}
yourdf=pd.concat([d.get(x) for x in np.random.choice(len(d),2,replace=False)])
yourdf
Out[986]:
col1 col2
4 5 e
5 6 f
6 7 g
7 8 h
0 1 a
1 2 b
2 3 c
3 4 d
正如你说的,你有4个序列的数据,那么数据帧的长度应该是4的倍数。如果你的数据是3个序列,请在代码中将4改为3 .
>>> import pandas as pd
>>> import numpy as np
正在创建 table:
>>> df = pd.DataFrame({'col1':[1,2,3,4,5,6,7,8],'col2':['a','b','c','d','e','f','g','h']})
>>> df
col1 col2
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
5 6 f
6 7 g
7 8 h
>>> df.shape[0]
8
正在创建洗牌列表:
>>> np_range = np.arange(0,df.shape[0])
>>> np_range
array([0, 1, 2, 3, 4, 5, 6, 7])
重塑和洗牌:
>>> np_range1 = np.reshape(np_range,(df.shape[0]/4,4))
>>> np_range1
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> np.random.shuffle(np_range1)
>>> np_range1
array([[4, 5, 6, 7],
[0, 1, 2, 3]])
>>> np_range2 = np.reshape(np_range1,(df.shape[0],))
>>> np_range2
array([4, 5, 6, 7, 0, 1, 2, 3])
正在选择数据:
>>> new_df = df.loc[np_range2]
>>> new_df
col1 col2
4 5 e
5 6 f
6 7 g
7 8 h
0 1 a
1 2 b
2 3 c
3 4 d
希望对您有所帮助!谢谢!
您可以以 4 个为一组重新洗牌,方法是...将索引分成四个一组,然后对它们进行洗牌。
示例:
df = pd.DataFrame(np.random.randint(10, size=(12, 2)))
a b
0 5 4
1 7 7
2 7 8
3 8 4
4 9 4
5 9 0
6 1 5
7 4 1
8 0 1
9 5 6
10 1 3
11 9 2
new_index = np.array(df.index).reshape(-1, 4)
np.random.shuffle(new_index) # shuffles array in-place
df = df.loc[new_index.reshape(-1)]
a b
8 0 1
9 5 6
10 1 3
11 9 2
4 9 4
5 9 0
6 1 5
7 4 1
0 5 4
1 7 7
2 7 8
3 8 4
我有一个包含预处理数据的数据框,这样每 4 行就是一个序列(稍后将被重塑并用于 lstm 训练)。
我想打乱数据帧,但我想保持每个行序列不变。例如:
a = [1,2,3,4,10,11,12,13,20,21,22,23]
会变成:a = [20,21,22,23,1,2,3,4,10,11,12,13]
.
df.sample(frac=1)
不够,因为它会破坏序列。
解决方案,感谢@Wen-Ben:
seq_length = 4
length_array = np.arange((df.shape[0]//seq_length)*seq_length)
trunc_data = df.head((df.shape[0]//seq_length)*seq_length)
d = {x : y for x, y in trunc_data.groupby(length_array//seq_length)}
yourdf = pd.concat([d.get(x) for x in np.random.choice(len(d),len(d.keys()),replace=False)])
这是你需要的吗,np.random.choice
d={x : y for x, y in df.groupby(np.arange(len(df))//4)}
yourdf=pd.concat([d.get(x) for x in np.random.choice(len(d),2,replace=False)])
yourdf
Out[986]:
col1 col2
4 5 e
5 6 f
6 7 g
7 8 h
0 1 a
1 2 b
2 3 c
3 4 d
正如你说的,你有4个序列的数据,那么数据帧的长度应该是4的倍数。如果你的数据是3个序列,请在代码中将4改为3 .
>>> import pandas as pd
>>> import numpy as np
正在创建 table:
>>> df = pd.DataFrame({'col1':[1,2,3,4,5,6,7,8],'col2':['a','b','c','d','e','f','g','h']})
>>> df
col1 col2
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
5 6 f
6 7 g
7 8 h
>>> df.shape[0]
8
正在创建洗牌列表:
>>> np_range = np.arange(0,df.shape[0])
>>> np_range
array([0, 1, 2, 3, 4, 5, 6, 7])
重塑和洗牌:
>>> np_range1 = np.reshape(np_range,(df.shape[0]/4,4))
>>> np_range1
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> np.random.shuffle(np_range1)
>>> np_range1
array([[4, 5, 6, 7],
[0, 1, 2, 3]])
>>> np_range2 = np.reshape(np_range1,(df.shape[0],))
>>> np_range2
array([4, 5, 6, 7, 0, 1, 2, 3])
正在选择数据:
>>> new_df = df.loc[np_range2]
>>> new_df
col1 col2
4 5 e
5 6 f
6 7 g
7 8 h
0 1 a
1 2 b
2 3 c
3 4 d
希望对您有所帮助!谢谢!
您可以以 4 个为一组重新洗牌,方法是...将索引分成四个一组,然后对它们进行洗牌。
示例:
df = pd.DataFrame(np.random.randint(10, size=(12, 2)))
a b
0 5 4
1 7 7
2 7 8
3 8 4
4 9 4
5 9 0
6 1 5
7 4 1
8 0 1
9 5 6
10 1 3
11 9 2
new_index = np.array(df.index).reshape(-1, 4)
np.random.shuffle(new_index) # shuffles array in-place
df = df.loc[new_index.reshape(-1)]
a b
8 0 1
9 5 6
10 1 3
11 9 2
4 9 4
5 9 0
6 1 5
7 4 1
0 5 4
1 7 7
2 7 8
3 8 4