本文介绍了将数据帧行附加到列的矢量化方法,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框如下:

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}})

给出:

>>>dfa b c1 0 00 0 11 0 11 1 0

对于数据框中的每一行,我想添加一个 0s 的新列,并且对于数据框中的每一列,我想添加一个新的 0 行s.

到目前为止,我已尝试通过以下方式解决此问题:

edges = df.columns对于 df.index 中的 i:df[i] = [0 for _ in range(len(df.index))]对于边缘中的 e:df = df.append(pd.Series({c:0 for c in df.columns},name=e))

产生所需的输出:

>>>dfa b c d e f gd 1 0 0 0 0 0 00 0 1 0 0 0 0f 1 0 1 0 0 0 0克 1 1 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 0

有矢量化的替代方案吗?

解决方案

使用 DataFrame.reindex 带有 columnsindex 参数,新值应该由 Index.append:

df1 = df.reindex(columns=df.columns.append(df.index),索引=df.index.append(df.columns),填充值 = 0)打印 (df1)a b c d e f gd 1 0 0 0 0 0 00 0 1 0 0 0 0f 1 0 1 0 0 0 0克 1 1 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 0

或者通过索引.联合:

df1 = df.reindex(columns=df.columns.union(df.index, sort=False),index=df.index.union(df.columns, sort=False),填充值 = 0)打印 (df1)a b c d e f g0 0 0 0 0 0 00 0 0 0 0 0 00 0 0 0 0 0 0d 1 0 0 0 0 0 00 0 1 0 0 0 0f 1 0 1 0 0 0 0克 1 1 0 0 0 0 0

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?

解决方案

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

这篇关于将数据帧行附加到列的矢量化方法,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:03