米脂
考虑给定的数据帧和数组,df1 = pd.DataFrame([1,0,0,1,1,0])df2 = pd.DataFrame([1,0,0,1])df3 = pd.DataFrame([1,0,0,1,1])arr = np.array([3, 1, 1, 2, 4, 0, 6, 7, 2, 1, 3, 2, 5, 5, 5])您可以根据给定的数据帧numpy.split将 an 拆分array为多个子数组。然后,您可以将这些数组作为列附加到它们各自的数据帧中。利用:dfs = [df1, df2, df3]def get_indices(dfs): """ Returns the split indices inside the array. """ indices = [0] for df in dfs: indices.append(len(df) + indices[-1]) return indices[1:-1]# split the given arr into multiple sections.sections = np.split(arr, get_indices(dfs))for df, s in zip(dfs, sections): df[1] = s # append the section of array to dataframe print(df)结果:# df1 0 10 1 31 0 12 0 13 1 24 1 45 0 0#df2 0 10 1 61 0 72 0 23 1 1# df3 0 10 1 31 0 22 0 53 1 54 1 5