【问题标题】:Using pandas dataframe, how to copy selected fragment data to other locations in the same dataframe使用 pandas 数据框,如何将选定的片段数据复制到同一数据框中的其他位置
【发布时间】:2021-02-28 10:37:45
【问题描述】:
【问题讨论】:
标签:
python
dataframe
slice
【解决方案1】:
这个问题比我预想的要难。我通过搜索网页找不到简单的方法,所以我可以转向繁琐的模式。解决方法如下:
...
columnsize = outer.shape[1] // 2
leftonly = outer[outer['_merge'] == "left_only"].copy()
column_numbers = [x + columnsize for x in range(columnsize - 1)]
leftonly.drop(leftonly.columns[column_numbers], axis=1, inplace=True)
column_numbers2 = np.append(column_numbers, [columnsize - 1, columnsize * 2 - 1])
rightonly1 = outer[outer['_merge'] == "right_only"].copy()
rightonly2 = rightonly1.iloc[:, column_numbers2].copy()
rightonly2.columns = leftonly.columns
df_combined = pd.concat([leftonly, rightonly2], axis=0, ignore_index=True
...
结果如下:
enter image description here