将数据分成 python 2.7 中的交替组
Splitting data into alternating groups in python 2.7
day city temperature windspeed event
2017-01-01 new york 32 6 Rain
2017-01-02 new york 36 7 Sunny
2017-01-03 new york 28 12 Snow
2017-01-04 new york 33 7 Sunny
2017-01-05 new york 31 7 Rain
2017-01-06 new york 33 5 Sunny
2017-01-07 new york 27 12 Rain
2017-01-08 new york 23 7 Rain
2017-01-01 mumbai 90 5 Sunny
2017-01-02 mumbai 85 12 Fog
2017-01-03 mumbai 87 15 Fog
2017-01-04 mumbai 92 5 Rain
2017-01-05 mumbai 89 7 Sunny
2017-01-06 mumbai 80 10 Fog
2017-01-07 mumbai 85 9 Sunny
2017-01-08 mumbai 89 8 Rain
2017-01-01 paris 45 20 Sunny
2017-01-02 paris 50 13 Cloudy
2017-01-03 paris 54 8 Cloudy
2017-01-04 paris 42 10 Cloudy
2017-01-05 paris 43 20 Sunny
2017-01-06 paris 48 4 Cloudy
2017-01-07 paris 40 14 Rain
2017-01-08 paris 42 15 Cloudy
2017-01-09 paris 53 8 Sunny
上面显示的是 .txt 文件。
我的目标是创建 4 个尽可能均匀分布的组,包含所有城市,这意味着每个组有 'new york'、'mumbai'、'paris'.
因为有25条数据,3组6行,1组7行。
我现在想到的是,由于数据已经按城市排序,我可以逐行读取文本文件,然后对于每一行,我将把它附加到 4 组(G1- G4) 交替模式。意思是说,第一行,它将附加到 G1,然后第二行附加到 G2,第三行附加到 G3,第四行附加到 G4,第五行附加回 G1,第六行附加到 G2,依此类推。这样可以保证所有组都拥有全部3个城市。
这样编码可行吗?
预期结果:
G1:
Row/Line 1 个,
第 5 行,
第 9 行,
G2:
第 2 行,
第 6 行,
第 10 行,
G3:
第 3 行,
第 7 行,
第 11 行,
G4:
第 4 行,
第 8 行,
第 12 行,依此类推。
我只保留行索引以便于解释
rows = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
然后就可以使用切片了
G1, G2, G3, G4 = [rows[i::4] for i in range(4)]
结果会是
G1 == [1, 5, 9, 13, 17, 21, 25]
G2 == [2, 6, 10, 14, 18, 22]
G3 == [3, 7, 11, 15, 19, 23]
G4 == [4, 8, 12, 16, 20, 24]]
由于您的输入已经排序,您可以将字符串拆分为一个列表,然后使用 4 的步骤对它们进行切片:
data = ''' 2017-01-01 new york 32 6 Rain
2017-01-02 new york 36 7 Sunny
2017-01-03 new york 28 12 Snow
2017-01-04 new york 33 7 Sunny
2017-01-05 new york 31 7 Rain
2017-01-06 new york 33 5 Sunny
2017-01-07 new york 27 12 Rain
2017-01-08 new york 23 7 Rain
2017-01-01 mumbai 90 5 Sunny
2017-01-02 mumbai 85 12 Fog
2017-01-03 mumbai 87 15 Fog
2017-01-04 mumbai 92 5 Rain
2017-01-05 mumbai 89 7 Sunny
2017-01-06 mumbai 80 10 Fog
2017-01-07 mumbai 85 9 Sunny
2017-01-08 mumbai 89 8 Rain
2017-01-01 paris 45 20 Sunny
2017-01-02 paris 50 13 Cloudy
2017-01-03 paris 54 8 Cloudy
2017-01-04 paris 42 10 Cloudy
2017-01-05 paris 43 20 Sunny
2017-01-06 paris 48 4 Cloudy
2017-01-07 paris 40 14 Rain
2017-01-08 paris 42 15 Cloudy
2017-01-09 paris 53 8 Sunny'''
lines = data.splitlines()
groups = [lines[i::4] for i in range(4)]
for g in groups:
print(g)
这输出:
[' 2017-01-01 new york 32 6 Rain', ' 2017-01-05 new york 31 7 Rain', ' 2017-01-01 mumbai 90 5 Sunny', ' 2017-01-05 mumbai 89 7 Sunny', ' 2017-01-01 paris 45 20 Sunny', ' 2017-01-05 paris 43 20 Sunny', ' 2017-01-09 paris 53 8 Sunny']
[' 2017-01-02 new york 36 7 Sunny', ' 2017-01-06 new york 33 5 Sunny', ' 2017-01-02 mumbai 85 12 Fog', ' 2017-01-06 mumbai 80 10 Fog', ' 2017-01-02 paris 50 13 Cloudy', ' 2017-01-06 paris 48 4 Cloudy']
[' 2017-01-03 new york 28 12 Snow', ' 2017-01-07 new york 27 12 Rain', ' 2017-01-03 mumbai 87 15 Fog', ' 2017-01-07 mumbai 85 9 Sunny', ' 2017-01-03 paris 54 8 Cloudy', ' 2017-01-07 paris 40 14 Rain']
[' 2017-01-04 new york 33 7 Sunny', ' 2017-01-08 new york 23 7 Rain', ' 2017-01-04 mumbai 92 5 Rain', ' 2017-01-08 mumbai 89 8 Rain', ' 2017-01-04 paris 42 10 Cloudy', ' 2017-01-08 paris 42 15 Cloudy']
您可以使用 pandas
和一些数学运算来复制您的组。
n, r = df.shape[0] // 4, df.shape[0] % 4
df['group'] = [1,2,3,4]*n + [1,2,3,4][:r]
day city temperature windspeed event group
0 2017-01-01 new york 32 6 Rain 1
1 2017-01-02 new york 36 7 Sunny 2
2 2017-01-03 new york 28 12 Snow 3
3 2017-01-04 new york 33 7 Sunny 4
4 2017-01-05 new york 31 7 Rain 1
5 2017-01-06 new york 33 5 Sunny 2
6 2017-01-07 new york 27 12 Rain 3
7 2017-01-08 new york 23 7 Rain 4
8 2017-01-01 mumbai 90 5 Sunny 1
9 2017-01-02 mumbai 85 12 Fog 2
10 2017-01-03 mumbai 87 15 Fog 3
11 2017-01-04 mumbai 92 5 Rain 4
12 2017-01-05 mumbai 89 7 Sunny 1
13 2017-01-06 mumbai 80 10 Fog 2
14 2017-01-07 mumbai 85 9 Sunny 3
15 2017-01-08 mumbai 89 8 Rain 4
16 2017-01-01 paris 45 20 Sunny 1
17 2017-01-02 paris 50 13 Cloudy 2
18 2017-01-03 paris 54 8 Cloudy 3
19 2017-01-04 paris 42 10 Cloudy 4
20 2017-01-05 paris 43 20 Sunny 1
21 2017-01-06 paris 48 4 Cloudy 2
22 2017-01-07 paris 40 14 Rain 3
23 2017-01-08 paris 42 15 Cloudy 4
24 2017-01-09 paris 53 8 Sunny 1
day city temperature windspeed event
2017-01-01 new york 32 6 Rain
2017-01-02 new york 36 7 Sunny
2017-01-03 new york 28 12 Snow
2017-01-04 new york 33 7 Sunny
2017-01-05 new york 31 7 Rain
2017-01-06 new york 33 5 Sunny
2017-01-07 new york 27 12 Rain
2017-01-08 new york 23 7 Rain
2017-01-01 mumbai 90 5 Sunny
2017-01-02 mumbai 85 12 Fog
2017-01-03 mumbai 87 15 Fog
2017-01-04 mumbai 92 5 Rain
2017-01-05 mumbai 89 7 Sunny
2017-01-06 mumbai 80 10 Fog
2017-01-07 mumbai 85 9 Sunny
2017-01-08 mumbai 89 8 Rain
2017-01-01 paris 45 20 Sunny
2017-01-02 paris 50 13 Cloudy
2017-01-03 paris 54 8 Cloudy
2017-01-04 paris 42 10 Cloudy
2017-01-05 paris 43 20 Sunny
2017-01-06 paris 48 4 Cloudy
2017-01-07 paris 40 14 Rain
2017-01-08 paris 42 15 Cloudy
2017-01-09 paris 53 8 Sunny
上面显示的是 .txt 文件。
我的目标是创建 4 个尽可能均匀分布的组,包含所有城市,这意味着每个组有 'new york'、'mumbai'、'paris'.
因为有25条数据,3组6行,1组7行。
我现在想到的是,由于数据已经按城市排序,我可以逐行读取文本文件,然后对于每一行,我将把它附加到 4 组(G1- G4) 交替模式。意思是说,第一行,它将附加到 G1,然后第二行附加到 G2,第三行附加到 G3,第四行附加到 G4,第五行附加回 G1,第六行附加到 G2,依此类推。这样可以保证所有组都拥有全部3个城市。
这样编码可行吗?
预期结果:
G1: Row/Line 1 个, 第 5 行, 第 9 行,
G2: 第 2 行, 第 6 行, 第 10 行,
G3: 第 3 行, 第 7 行, 第 11 行,
G4: 第 4 行, 第 8 行, 第 12 行,依此类推。
我只保留行索引以便于解释
rows = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
然后就可以使用切片了
G1, G2, G3, G4 = [rows[i::4] for i in range(4)]
结果会是
G1 == [1, 5, 9, 13, 17, 21, 25]
G2 == [2, 6, 10, 14, 18, 22]
G3 == [3, 7, 11, 15, 19, 23]
G4 == [4, 8, 12, 16, 20, 24]]
由于您的输入已经排序,您可以将字符串拆分为一个列表,然后使用 4 的步骤对它们进行切片:
data = ''' 2017-01-01 new york 32 6 Rain
2017-01-02 new york 36 7 Sunny
2017-01-03 new york 28 12 Snow
2017-01-04 new york 33 7 Sunny
2017-01-05 new york 31 7 Rain
2017-01-06 new york 33 5 Sunny
2017-01-07 new york 27 12 Rain
2017-01-08 new york 23 7 Rain
2017-01-01 mumbai 90 5 Sunny
2017-01-02 mumbai 85 12 Fog
2017-01-03 mumbai 87 15 Fog
2017-01-04 mumbai 92 5 Rain
2017-01-05 mumbai 89 7 Sunny
2017-01-06 mumbai 80 10 Fog
2017-01-07 mumbai 85 9 Sunny
2017-01-08 mumbai 89 8 Rain
2017-01-01 paris 45 20 Sunny
2017-01-02 paris 50 13 Cloudy
2017-01-03 paris 54 8 Cloudy
2017-01-04 paris 42 10 Cloudy
2017-01-05 paris 43 20 Sunny
2017-01-06 paris 48 4 Cloudy
2017-01-07 paris 40 14 Rain
2017-01-08 paris 42 15 Cloudy
2017-01-09 paris 53 8 Sunny'''
lines = data.splitlines()
groups = [lines[i::4] for i in range(4)]
for g in groups:
print(g)
这输出:
[' 2017-01-01 new york 32 6 Rain', ' 2017-01-05 new york 31 7 Rain', ' 2017-01-01 mumbai 90 5 Sunny', ' 2017-01-05 mumbai 89 7 Sunny', ' 2017-01-01 paris 45 20 Sunny', ' 2017-01-05 paris 43 20 Sunny', ' 2017-01-09 paris 53 8 Sunny']
[' 2017-01-02 new york 36 7 Sunny', ' 2017-01-06 new york 33 5 Sunny', ' 2017-01-02 mumbai 85 12 Fog', ' 2017-01-06 mumbai 80 10 Fog', ' 2017-01-02 paris 50 13 Cloudy', ' 2017-01-06 paris 48 4 Cloudy']
[' 2017-01-03 new york 28 12 Snow', ' 2017-01-07 new york 27 12 Rain', ' 2017-01-03 mumbai 87 15 Fog', ' 2017-01-07 mumbai 85 9 Sunny', ' 2017-01-03 paris 54 8 Cloudy', ' 2017-01-07 paris 40 14 Rain']
[' 2017-01-04 new york 33 7 Sunny', ' 2017-01-08 new york 23 7 Rain', ' 2017-01-04 mumbai 92 5 Rain', ' 2017-01-08 mumbai 89 8 Rain', ' 2017-01-04 paris 42 10 Cloudy', ' 2017-01-08 paris 42 15 Cloudy']
您可以使用 pandas
和一些数学运算来复制您的组。
n, r = df.shape[0] // 4, df.shape[0] % 4
df['group'] = [1,2,3,4]*n + [1,2,3,4][:r]
day city temperature windspeed event group
0 2017-01-01 new york 32 6 Rain 1
1 2017-01-02 new york 36 7 Sunny 2
2 2017-01-03 new york 28 12 Snow 3
3 2017-01-04 new york 33 7 Sunny 4
4 2017-01-05 new york 31 7 Rain 1
5 2017-01-06 new york 33 5 Sunny 2
6 2017-01-07 new york 27 12 Rain 3
7 2017-01-08 new york 23 7 Rain 4
8 2017-01-01 mumbai 90 5 Sunny 1
9 2017-01-02 mumbai 85 12 Fog 2
10 2017-01-03 mumbai 87 15 Fog 3
11 2017-01-04 mumbai 92 5 Rain 4
12 2017-01-05 mumbai 89 7 Sunny 1
13 2017-01-06 mumbai 80 10 Fog 2
14 2017-01-07 mumbai 85 9 Sunny 3
15 2017-01-08 mumbai 89 8 Rain 4
16 2017-01-01 paris 45 20 Sunny 1
17 2017-01-02 paris 50 13 Cloudy 2
18 2017-01-03 paris 54 8 Cloudy 3
19 2017-01-04 paris 42 10 Cloudy 4
20 2017-01-05 paris 43 20 Sunny 1
21 2017-01-06 paris 48 4 Cloudy 2
22 2017-01-07 paris 40 14 Rain 3
23 2017-01-08 paris 42 15 Cloudy 4
24 2017-01-09 paris 53 8 Sunny 1