使用Python将索引设置为csv文件中重复行值的组
我是python新手。我想将索引设置为特定的行元素集,这些元素对每个组都是重复的。 我的数据帧: 在这里,列表被创建为idx,索引值被写入maunally 但对于一个每组1000多个索引(随机值)的更大矩阵来说,这将是一项非常困难的任务 我想要的是,当我读取CSV文件时,应该选择行中的第一个值,直到相同的值出现在同一索引中,它应该被视为1组,并且第一组的索引必须存储在idx 例如:在我的小版本数据集中,从第一列使用Python将索引设置为csv文件中重复行值的组,python,pandas,list,numpy,group-by,Python,Pandas,List,Numpy,Group By,我是python新手。我想将索引设置为特定的行元素集,这些元素对每个组都是重复的。 我的数据帧: 在这里,列表被创建为idx,索引值被写入maunally 但对于一个每组1000多个索引(随机值)的更大矩阵来说,这将是一项非常困难的任务 我想要的是,当我读取CSV文件时,应该选择行中的第一个值,直到相同的值出现在同一索引中,它应该被视为1组,并且第一组的索引必须存储在idx 例如:在我的小版本数据集中,从第一列索引中,第一个值即4,10,37109133是我第一组的索引。对于下一组,这些值以相
索引中,第一个值即4,10,37109133
是我第一组的索引。对于下一组,这些值以相同的顺序重复。
只有它们不应在代码中手动输入。如果下一次出现4
,则应将一组视为一组。因此,索引值4到133必须视为idx下的1组。这是因为我需要idx来获取代码的进一步部分
我的实际数据集每个组有1000多个索引值。因此,idx必须自动获取组中的所有值
# getting some dimensions and sorting the data
row_idx_length = len(idx)
group_length = len(data_dict['0_deg'])
number_of_groups = len(data_dict.keys())
idx = idx*number_of_groups
data_arr = np.zeros((group_length,number_of_groups),dtype=np.int32)
我相信您需要使用整数除法来计算重复次数:
df.index = np.tile(idx, len(df.index) // len(idx))
但是如果get error(N
和M
是一些整数):
ValueError:长度不匹配:预期轴有N个元素,新值有M个元素
这意味着在最后一个组中缺少一些元素,因此通过1
添加下一个组,并通过数据帧的索引的长度进行过滤:
#added 150 to idx list
idx =[4,10,37,109,133,150]
df.index = np.tile(idx, len(df.index) // len(idx) + 1)[:len(df.index)]
print (df)
0_deg 10_deg 20_deg 30_deg
4 2 12 23 12
10 11 -21 -10 9
37 21 11 3 -5
109 -17 1 20 4
133 5 4 -41 9
150 40 -2 13 -46
4 22 33 10 1
10 7 53 5 -8
37 20 18 -9 -27
109 -6 10 7 3
133 -6 -3 -4 -9
150 -6 -1 -21 -14
4 24 23 14 15
10 21 18 -26 -6
37 20 23 -31 14
109 61 8 9 7
133 21 11 1 -11
150 5 -25 -15 5
4 2 21 3 19
10 17 -14 -6 -4
如果我正确理解了您的问题,那么您需要实际获取给定数据帧的idx,并使用类似的索引设置
data_dict ={'0_deg': [2, 11, 21, -17, 5, 40, 22, 7, 20, -6, -6, -6, 24, 21, 20, 61, 21, 5, 2, 17],
'10_deg': [12, -21, 11, 1, 4, -2, 33, 53, 18, 10, -3, -1, 23, 18, 23, 8, 11, -25, 21, -14],
'20_deg': [23, -10, 3, 20, -41, 13, 10, 5, -9, 7, -4, -21, 14, -26, -31, 9, 1, -15, 3, -6],
'30_deg': [12, 9, -5, 4, 9, -46, 1, -8, -27, 3, -9, -14, 15, -6, 14, 7, -11, 5, 19, -4]}
import pandas as pd
df = pd.DataFrame(data_dict)
idx =[4,10,37,109,133]
df.index = idx * (len(df)//len(idx))
print(df)
#Output:
0_deg 10_deg 20_deg 30_deg groups
4 2 12 23 12 1
10 11 -21 -10 9 1
37 21 11 3 -5 1
109 -17 1 20 4 1
133 5 4 -41 9 1
4 40 -2 13 -46 2
10 22 33 10 1 2
37 7 53 5 -8 2
109 20 18 -9 -27 2
133 -6 10 7 3 2
4 -6 -3 -4 -9 3
10 -6 -1 -21 -14 3
37 24 23 14 15 3
109 21 18 -26 -6 3
133 20 23 -31 14 3
4 61 8 9 7 4
10 21 11 1 -11 4
37 5 -25 -15 5 4
109 2 21 3 19 4
133 17 -14 -6 -4 4
设置完成后,只需计算第0个值重复的位置。您可以直接计算它,但为进一步的计算创建一个附加列也可能更有用
temp = df.index == df.index[0]
groups = temp.cumsum()
idx_out = list(df.index[groups == 1])
print(idx_out) #[4, 10, 37, 109, 133]
#Optional:
df['groups'] = groups #can be very useful for aggregations and groupby calculations
最后一个组是否可能在重复索引中缺少一些值?每个组有1000多个元素。那么如何为idx动态设置它们呢?我不想手动创建像idx=[4,10,37109133150]
@anky_91-hmmm这样的列表,这是一个很难回答的问题,但是如果多元素numpy应该更快。@AkshayK.-countidx如何?在我的一个数据帧中,索引以值4
开始,如图所示。在另一种情况下,它可能从其他值开始。但不管值是什么,它都会对每个组重复,而且每个组的索引范围是固定的。例如,从4
到“`133``元素是固定的。@AkshayK.-不确定是否理解,idx
的解析值还有许多其他数据帧?如果可能的话,您可以在新问题中创建吗?在您的代码中,您已经为索引idx=[4,10,37109133]
创建了一个列表。是否可以不输入所有值,只输入第一个值,例如4,这样idx将存储从4开始的第一列的值,直到该列中出现下一个4?您能否详细说明“idx将存储从4开始的第一列的值,直到该列中出现下一个4”?我以为你已经“得到”了这样的索引,而我最初的idx
只是“设置”的一部分。你还没有得到某种重复的索引吗?如果你只使用答案的第二部分,假设你得到的是一个索引集为那样的数据帧,它会给出该数据帧的idx\u out
值。我得到的重复间隔相同,但这只是5个索引值的情况,我的原始数据集有5000个值,所以在5000多个值之后,4会重复。那么,如何为这种情况指定idx=[]
?您当前的问题是否很好地代表了您的需求?我对你的理解是否正确@阿克谢。
data_dict ={'0_deg': [2, 11, 21, -17, 5, 40, 22, 7, 20, -6, -6, -6, 24, 21, 20, 61, 21, 5, 2, 17],
'10_deg': [12, -21, 11, 1, 4, -2, 33, 53, 18, 10, -3, -1, 23, 18, 23, 8, 11, -25, 21, -14],
'20_deg': [23, -10, 3, 20, -41, 13, 10, 5, -9, 7, -4, -21, 14, -26, -31, 9, 1, -15, 3, -6],
'30_deg': [12, 9, -5, 4, 9, -46, 1, -8, -27, 3, -9, -14, 15, -6, 14, 7, -11, 5, 19, -4]}
import pandas as pd
df = pd.DataFrame(data_dict)
idx =[4,10,37,109,133]
df.index = idx * (len(df)//len(idx))
print(df)
#Output:
0_deg 10_deg 20_deg 30_deg groups
4 2 12 23 12 1
10 11 -21 -10 9 1
37 21 11 3 -5 1
109 -17 1 20 4 1
133 5 4 -41 9 1
4 40 -2 13 -46 2
10 22 33 10 1 2
37 7 53 5 -8 2
109 20 18 -9 -27 2
133 -6 10 7 3 2
4 -6 -3 -4 -9 3
10 -6 -1 -21 -14 3
37 24 23 14 15 3
109 21 18 -26 -6 3
133 20 23 -31 14 3
4 61 8 9 7 4
10 21 11 1 -11 4
37 5 -25 -15 5 4
109 2 21 3 19 4
133 17 -14 -6 -4 4
temp = df.index == df.index[0]
groups = temp.cumsum()
idx_out = list(df.index[groups == 1])
print(idx_out) #[4, 10, 37, 109, 133]
#Optional:
df['groups'] = groups #can be very useful for aggregations and groupby calculations