[英]How to index the elements in a list for pandas?
我用 np.shape(sample_size, ) 创建了一个 numpy 数组,然后我使用 pandas dataframe 来显示数据。 我想请问一下,如何为列表添加子列索引,例如:示例 1、示例 2、示例 3 等?
low = 0
high = 500
sample_size = 5
def get_numbers(low, high, sample_size):
return random.sample(range(low, high), sample_size)
p_one = np.array(get_numbers(low, high, sample_size), dtype = int)
p_two = np.array(get_numbers(low, high, sample_size), dtype = int)
p_three = np.array(get_numbers(low, high, sample_size), dtype = int)
p_four = np.array(get_numbers(low, high, sample_size), dtype = int)
p_five = np.array(get_numbers(low, high, sample_size), dtype = int)
for idn in range(0,n): #------------------n+1 for the last process step
p = [p_one, p_two, p_three, p_four, p_five]
df_rawdata = pd.DataFrame(list(zip(p)),columns =['Processing'])
例子
我们需要代码中最小且可重现的示例
df = pd.DataFrame([[[1, 2, 3]], [[4, 5, 6]]], index=['step1', 'step2'], columns=['process'])
df
process
step1 [1, 2, 3]
step2 [4, 5, 6]
代码
第一的。 将列表扩展到列
df1 = df['process'].apply(pd.Series).rename(columns=lambda x: f'sp {x+1}')
df1
sp 1 sp 2 sp 3
step1 1 2 3
step2 4 5 6
第二。 制作多索引
out = pd.concat([df1], keys=['process'], axis=1)
out
process
sp 1 sp 2 sp 3
step1 1 2 3
step2 4 5 6
更新
或使用以下示例代码:
df = pd.DataFrame([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], index=['step1', 'step2'], columns=['process1', 'process2'])
df
process1 process2
step1 [1, 2] [3, 4]
step2 [5, 6] [7, 8]
out = (df.stack()
.apply(pd.Series).rename(columns=lambda x: f'sp {x+1}')
.unstack().swaplevel(0, 1, axis=1).sort_index(axis=1))
out
process1 process2
sp 1 sp 2 sp 1 sp 2
step1 1 2 3 4
step2 5 6 7 8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.