熊猫,根据行中的值创建列
Posted
技术标签:
【中文标题】熊猫,根据行中的值创建列【英文标题】:Pandas, create columns based on value in row 【发布时间】:2022-01-01 01:29:24 【问题描述】:我有以下数据框(这只是一行):
X0 X1 X2 X3 X4 X5 X6 X8
32 23 17 0 3 24 9 14
我想将 X8 列中的值 14 映射到以下列表mylist
:
[ 0.06405287, -0.1176078 , -0.06206927, 0.08389127, -0.18036067,
0.35158703, -0.0928449 , -0.0974429 , -0.06705306, -0.17196381,
-0.03776502, 0.09204011, 0.47813812, 0.16258538, 0.2699648 ,
0.07496626, -0.09791522, -0.31499937, -0.24898018, 0.06126055,
0.13187763, 0.21042736, -0.1585868 , 0.08355565, -0.13935572,
0.12408883, 0.2043313 , -0.12544186, -0.09223691, 0.00720569]
为了这个问题,这里有一个创建一个数据帧的 for 循环。
col =
col['X8'] = #
col['X8']['14']= # just creating the double nest for the sake of the example, this would of course be 3 for loops because I need to match each unique value in each column to its corresponding list of 30 values
for i, x in mylist:
col['X8']['14'][i] = x
colx814 = pd.DataFrame.from_dict(col['X8']['14'],orient='index').T
现在我有一个如下所示的数据框:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
0.064053 -0.117608 -0.062069 0.083891 -0.180361 0.351587 -0.092845 -0.097443 -0.067053 -0.171964 -0.037765 0.09204 0.478138 0.162585 0.269965 0.074966 -0.097915 -0.314999 -0.24898 0.061261 0.131878 0.210427 -0.158587 0.083556 -0.139356 0.124089 0.204331 -0.125442 -0.092237 0.007206
如何将其加入我的原始数据框?我的猜测是map+apply+concat,但我不确定如何有效地组合它们。另一种解决方案是将每一列复制 30 次,然后对每一列进行迭代+替换?我搜索并找不到这个问题的答案,所以我想我会发布它以防有人有答案,如果没有自己回答。这就是我要找的:
X0 X1 X2 X3 X4 X5 X6 X8 X8_0 X8_1 X8_2 ...etc
32 23 17 0 3 24 9 14 mylist[0] mylist[1] mylist[2] ...etc
当然,每一列都有自己的 30 个 XN_n 列。
【问题讨论】:
【参考方案1】:weights =
for index, x in enumerate(encoded.columns):
weights[x] =
for id, val in enumerate(encoded[x].unique()):
weights[x][val] =
for weightid, weightval in enumerate(model_full.get_layer(embeddings[index]).get_weights()[0][id]):
weights[x][val][weightid] = weightval
mappedembeddings = pd.DataFrame()
encodedindex = []
for row in encoded.iterrows():
encodedindex.append(row[0])
df0 = pd.DataFrame()
for k, v in row[1].to_dict().items():
names = []
for z in weights[k][v].keys():
names.append(str(k)+'_'+str(z))
tempdf = pd.DataFrame([weights[k][v]])
tempdf.columns = names
df0 = pd.concat([df0,tempdf],axis=1)
mappedembeddings = pd.concat([mappedembeddings,df0],axis=0)
mappedembeddings.index = encodedindex
【讨论】:
以上是关于熊猫,根据行中的值创建列的主要内容,如果未能解决你的问题,请参考以下文章