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 0
s , and for every column in the dataframe, I would like to add a new row of 0
s.
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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With