Python 如何将阵列转换为数据帧?
我有一个数据帧: 身份证件 239200202 14700993 1153709258720067584Python 如何将阵列转换为数据帧?,python,python-3.x,pandas,dataframe,csv,Python,Python 3.x,Pandas,Dataframe,Csv,我有一个数据帧: 身份证件 239200202 14700993 1153709258720067584 根据你在问题中给出的提示 您可以将列名Bot添加到测试数据框中,如下所示: new_pred=['bot'如果x==1,则为'Not bot'(在pred_logreg_测试中为x设置)] 测试['Bot']=列表(新建) 以下是上述问题的解决方案 array = [1,1,0] df['BOT']=df.loc[df['ID'].isin(array)] 最好在这里与pd.concat一
根据你在问题中给出的提示 您可以将列名
Bot
添加到测试数据框中,如下所示:
new_pred=['bot'如果x==1,则为'Not bot'(在pred_logreg_测试中为x设置)]
测试['Bot']=列表(新建)
以下是上述问题的解决方案
array = [1,1,0]
df['BOT']=df.loc[df['ID'].isin(array)]
最好在这里与pd.concat一起使用,将这2个df合并为一个 另外,在使用数据帧时,要不惜任何代价避免iTerrow,因为它的速度要慢得多 例如:
import pandas as pd
import numpy as np
df = pd.DataFrame({'ID': [100, 101, 102]})
bot_not_bot = np.array([1,0,1])
df = pd.concat([df, pd.DataFrame({'bot/not bot': bot_not_bot})], axis=1)
不要使用较慢的ItError,而是使用apply在较大规模的数据帧上获得更快的结果
df['bot/not bot'] = df['bot/not bot'].apply(lambda x: 'Bot' if x else 'Not Bot')
这是使用数据帧的正确方法,避免出现iterrows使用数组索引:
df = pd.DataFrame({'UserID': [239200202, 14700993, 1153709258720067584]})
is_bot = np.array([1,1,0])
df['Bot'] = np.array(['not bot', 'bot'])[is_bot]
test\u dataframe.csv是否仅包含ID?数组的名称是什么?yes test\u dataframe.csv是否仅包含ID。我删除了其余的列,因为我不需要它。数组是pred_logreg_test=logreg.predict(test_scaled)的输出。我预测输出数组的id是否是机器人。您可以使用pandas,对于数组/列表,您可以执行列表理解arr=[1,1,0]
arr=['bot'如果x==1,则为'Not'bot'对于arr中的x]
非常感谢,它的效果非常好