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

霸气de小男生 提交于 2020-02-24 04:48:46

问题


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?


回答1:


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



回答2:


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



回答3:


Create a dictionary from fromkeys then unpack it, then use assign and T then assign then T:

print(df.assign(**dict.fromkeys(df.index, 0)).T.assign(**dict.fromkeys(df.columns, 0)).T)

Output:

   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


来源:https://stackoverflow.com/questions/59874966/vectorised-method-to-append-dataframe-rows-to-columns-and-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!