【发布时间】:2021-08-21 06:23:43
【问题描述】:
我有一个数据框:
| ID |
|---|
| 239200202 |
| 14700993 |
| 1153709258720067584 |
并以数组形式 [1,1,0] 输出 id 是否为机器人 如何将其组合成一个数据框,例如:
| ID | Bot |
|---|---|
| 239200202 | bot |
| 14700993 | bot |
| 1153709258720067584 | Not bot |
我试过这段代码,但没有用:
test = pd.read_csv('./user_data/user_lookup/dataset/test_dataframe.csv', index_col=1)
df = pd.DataFrame(columns=['UserID','Bot/Not'])
for index,row in test.iterrows():
if test[index] == 1:
df.loc[index,['UserID']] = test['User ID']
df.loc[index,['Bot/Not']] = 'Bot'
if test[index] == 0:
df.loc[index, ['UserID']] = test['User ID']
df.loc[index, ['Bot/Not']] = 'Not-Bot'
print(df)
如果有人可以帮助我,那就太好了。 谢谢
【问题讨论】:
-
test_dataframe.csv是否仅包含 ID? -
数组的名称是什么?
-
是的 test_dataframe.csv 只有 ids。我删除了其余的列,因为我不需要它。
-
array 是一个输出:pred_logreg_test = logreg.predict(test_scaled) 我正在预测输出数组的 id 是否是机器人
-
你可以使用pandas assign,对于你的数组/列表你可以做一个列表理解
arr =[1,1,0]arr = ['Bot' if x==1 else 'Not-Bot' for x in arr]
标签: python python-3.x pandas dataframe csv