Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorised method to append dataframe rows to columns and vice-versa

My dataframe is as follows:

df = pd.DataFrame({'a': {'d': 1, 'e': 0, 'f': 1, 'g': 1},
                   'b': {'d': 0, 'e': 0, 'f': 0, 'g': 1},
                   'c': {'d': 0, 'e': 1, 'f': 1, 'g': 0}})

which gives:

>>> df
   a  b  c
d  1  0  0
e  0  0  1
f  1  0  1
g  1  1  0

For every row in the dataframe, I would like to add a new column of 0s , and for every column in the dataframe, I would like to add a new row of 0s.

I've attempted to solve this problem so far in the following way:

edges = df.columns

for i in df.index:
    df[i] = [0 for _ in range(len(df.index))]

for e in edges:
    df = df.append(pd.Series({c:0 for c in df.columns},name=e))

Which results in the desired output:

>>> df
   a  b  c  d  e  f  g
d  1  0  0  0  0  0  0
e  0  0  1  0  0  0  0
f  1  0  1  0  0  0  0
g  1  1  0  0  0  0  0
a  0  0  0  0  0  0  0
b  0  0  0  0  0  0  0
c  0  0  0  0  0  0  0

Is there a vectorised alternative?

like image 209
CDJB Avatar asked Dec 23 '22 19:12

CDJB


2 Answers

Here's one way using reindex:

(df.reindex(df.columns.append(df.index), 
           axis=1, 
           fill_value =0)
  .reindex(df.index.append(df.columns), 
           axis=0, 
           fill_value =0))

print(df_new)

   a  b  c  d  e  f  g
d  1  0  0  0  0  0  0
e  0  0  1  0  0  0  0
f  1  0  1  0  0  0  0
g  1  1  0  0  0  0  0
a  0  0  0  0  0  0  0
b  0  0  0  0  0  0  0
c  0  0  0  0  0  0  0
like image 88
yatu Avatar answered May 21 '23 22:05

yatu


Use DataFrame.reindex witn columns and index parameter, new values should be created by Index.append:

df1 = df.reindex(columns=df.columns.append(df.index), 
                 index=df.index.append(df.columns), 
                 fill_value = 0)
print (df1)
   a  b  c  d  e  f  g
d  1  0  0  0  0  0  0
e  0  0  1  0  0  0  0
f  1  0  1  0  0  0  0
g  1  1  0  0  0  0  0
a  0  0  0  0  0  0  0
b  0  0  0  0  0  0  0
c  0  0  0  0  0  0  0

Or by Index.union:

df1 = df.reindex(columns=df.columns.union(df.index, sort=False), 
                 index=df.index.union(df.columns, sort=False), 
                 fill_value = 0)
print (df1)
   a  b  c  d  e  f  g
a  0  0  0  0  0  0  0
b  0  0  0  0  0  0  0
c  0  0  0  0  0  0  0
d  1  0  0  0  0  0  0
e  0  0  1  0  0  0  0
f  1  0  1  0  0  0  0
g  1  1  0  0  0  0  0
like image 45
jezrael Avatar answered May 21 '23 22:05

jezrael