【发布时间】:2021-08-02 19:11:21
【问题描述】:
我正在尝试为 pandas 数据框的每个元素添加特征索引,以便每个元素都类似于 column_number:feature_value。例如,如果输入是这样的:
col1 col2
row1 1.23 2.24
row2 0.42 5.52
那么,输出数据框应该是这样的:
col1 col2
row1 1:1.23 2:2.24
row2 1:0.42 2:5.52
这里每个元素的dict的key和value分别是int和float类型。这是我的代码:
f1 = pd.DataFrame()
# removing the ground truth
X = feature_matrix1.drop(['Disease'], axis=1)
X1 = X.copy()
for i in tqdm(range(X.shape[0])):
for j in range(X.shape[1]):
X1.iloc[i,j] = {}
X1.iloc[i,j][i] = X1.iloc[i,j]
X 的大小为 1235x13160。但我收到Incompatible indexer with Series 的错误。请提出建议。
【问题讨论】:
标签: python pandas dataframe dictionary