I have a data frame "p" and a np array "b" as follow
p=pd.DataFrame([["j","k","h"],[4,5,6],[7,8,9]])
j k h
4 5 6
7 8 9
b=np.array([["a",10],["b",20],["c",30],["d",40]])
a 10
b 20
c 30
d 40
I want to insert the np array to the dataframe and make become as follow
j k h
a 10 10
b 20 20
c 30 30
d 40 40
Notice p is wider than b and b is longer than p
Basically:
- keep the first row of the p
- insert b into p from second row
- repeat second column of b to the rest of column of p
CodePudding user response:
IIUC, you can use:
pd.concat([p.iloc[[0]],
pd.DataFrame(np.hstack([b, b[:,[1]]]), columns=p.columns)],
ignore_index=True)
output:
0 1 2
0 j k h
1 a 10 10
2 b 20 20
3 c 30 30
4 d 40 40