尝试从一个Pandas数据框中获取列的一部分,转置该部分,然后将其插入到不同数据帧中大小相似的行切片中。两个数据框中的标签和索引均不同。对于大型数据帧,当前正在运行for循环,以逐个单元的方式复制每个单独的值,但是效率非常低。
除for循环外,尝试使用转置的.loc,.iloc,但未成功。 ivot,pivot_table,melt似乎在这里不适用,否则我无法确定如何将它们应用于这个看似简单的问题。
# Two dataframes here
import pandas as pd
import numpy as np
numRng = np.arange(20).reshape((5, 4))
df1 = pd.DataFrame(numRng)
newCols = ('A', 'B', 'C', 'D', 'E', 'F')
for newCol in newCols:
df1[newCol] = np.nan
numRng2 = np.arange(1000,976,-1).reshape((6, 4))
df2 = pd.DataFrame(numRng2)
df2.columns = ['M', 'N', 'O', 'P']
# From df1, trying to copy a column-slice, transpose it, and insert it
# into df2 row-slice, has no effect
df1.loc[1, 'B':'E'] = df2.loc[1:4, 'M'].transpose()
df1
# 'Manual' implementation to produce desired df1 geometry
df1.loc[1, 'B'] = 996
df1.loc[1, 'C'] = 992
df1.loc[1, 'D'] = 988
df1.loc[1, 'E'] = 984
df1
在上述示例df中,在df1 row1的B,C,D,E列中,行切片中的数字为996、992、988和984。
如何在不对每个值进行循环的情况下提取切片,转置和插入?
答案 0 :(得分:1)
将值转换为numpy数组以避免数据对齐-大熊猫尝试相互匹配索引和列,如果失败,则创建缺失值或不分配值:
#pandas 0.22+
df1.loc[1, 'B':'E'] = df2.loc[1:4, 'M'].transpose().to_numpy()
#pandas below
#df1.loc[1, 'B':'E'] = df2.loc[1:4, 'M'].transpose().values
print (df1)
0 1 2 3 A B C D E F
0 0 1 2 3 NaN NaN NaN NaN NaN NaN
1 4 5 6 7 NaN 996.0 992.0 988.0 984.0 NaN
2 8 9 10 11 NaN NaN NaN NaN NaN NaN
3 12 13 14 15 NaN NaN NaN NaN NaN NaN
4 16 17 18 19 NaN NaN NaN NaN NaN NaN