I'd like to create a pandas DataFrame with its values as concatenated index and column names. In the example below, I'd have the column name concatenated to the row name; however, a vice versa scenario is also possible.
A B C A AA AB AC B BA BB BC C CA CB CC
CodePudding user response:
Numpy's char
module
We want to use Numpy's broadcasting while using a string specific function.
Setup
I used an index of ['X', 'Y', 'Z']
to make more obvious that what I'm doing is accurate.
df = pd.DataFrame(index=['X', 'Y', 'Z'], columns=['A', 'B', 'C'])
Solution
idx = df.index.to_numpy().astype(str)
col = df.columns.to_numpy().astype(str)
df.loc[:, :] = np.char.add(idx[:, None], col)
df
A B C
X XA XB XC
Y YA YB YC
Z ZA ZB ZC
CodePudding user response:
Another option is to multiply the columns list by the length of df
; then use radd
on axis=0
to concatenate index names from the right:
df.loc[:,:] = [df.columns.astype(str).tolist()]*df.shape[0]
df = df.radd(df.index.astype(str), axis=0)
Or do the converse by multiplying the index list by the width of df
; then use add
on axis=1
to concatenate the column names:
df.loc[:,:] = list(zip(*[df.index.tolist()]*df.shape[1]))
df = df.add(df.columns, axis=1)
Output:
A B C
A AA AB AC
B BA BB BC
C CA CB CC