我有四个数据框如下:
这就是数据框的思想。现在我想复制多于一个的行 1. 并按如下方式分配值。
如果有任何意见,我将不胜感激。谢谢。
最佳答案
通过 pd.get_dumies
的一个选项:
df1 = (
pd.get_dummies(
df.set_index('Proxyid')
.mul(df.columns[1:])
.replace('', np.NAN)
.stack()
)
.reset_index().drop('level_1', 1)
)
result = df1.append(df[~df.Proxyid.isin(df1.Proxyid)])
输出:
如果您有额外的列,只需将它们添加到 set_index
中并使用:
df1 = df.set_index(['Proxyid', 'test'])
df1 = pd.get_dummies(df1.mul(df1.columns).replace('', np.NAN).stack()).reset_index()
result = df1.append(df[~df.Proxyid.isin(df1.Proxyid)])
关于pandas - 拆分行并分配值 pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67677731/