Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t将熊猫系列转换为int?正则表达式抛出错误?_Python_Numpy_Pandas - Fatal编程技术网

Python Can';t将熊猫系列转换为int?正则表达式抛出错误?

Python Can';t将熊猫系列转换为int?正则表达式抛出错误?,python,numpy,pandas,Python,Numpy,Pandas,我有一个如下所示的数据集 time a_id b_id c_id d_id probability 2015-01-02 237 9712 54 38 [0.194255020142] 2015-01-02 131 481 60 42 [0.23631604522] 2015-01-02

我有一个如下所示的数据集

  time               a_id      b_id        c_id     d_id  probability
  2015-01-02         237       9712        54       38  [0.194255020142]
  2015-01-02         131        481        60       42   [0.23631604522]
  2015-01-02         277       8842        57       46  [0.176149934661]
  2015-01-02         124       3664        95       48  [0.158623758706]
当前“概率”列的类型为object。我想把它转换成int,这样我就可以对它进行一些数学运算。我使用了以下代码

 df_total['probability] = df_total['probability'].astype(int)
但这给我带来了一个错误

ValueError: setting an array element with a sequence.
我通过子集和转换为list,将概率列从numpy数组转换为list。下面给出了代码

probability = probs[:,1:]
probability = probability.tolist()
我得到的是一个列表,元素被括在一个括号中?我不明白为什么


如何解决此问题?

看起来您当前的“概率”列值都是一个包含一个元素的列表

尝试以下方法:

def to_integer(row):
    prob = row['probability'][0] #0th element of the list is the actual float
    return int(prob)
df_total['probability'] = df_total.apply(lambda row: to_integer(row), axis = 1)

看起来您当前的“概率”列值都是一个包含一个元素的列表

尝试以下方法:

def to_integer(row):
    prob = row['probability'][0] #0th element of the list is the actual float
    return int(prob)
df_total['probability'] = df_total.apply(lambda row: to_integer(row), axis = 1)

假设概率当前为十进制形式,将其转换为int将导致值为零(例如int(.99)导致值为0)。在本例中,我假设您想要的是整数值99。要从每个列表中提取单个值,请执行以下操作:

df['probability'] = [int(100 * i[0]) if i else None for i in df.probability]

else无
零件在其中,以防丢失任何值。尝试在None上索引i[0]将抛出错误。

鉴于概率当前为十进制形式,将其转换为int将导致值为零(例如int(.99)结果为0)。在本例中,我假设您想要的是整数值99。要从每个列表中提取单个值,请执行以下操作:

df['probability'] = [int(100 * i[0]) if i else None for i in df.probability]

else无
零件在其中,以防丢失任何值。如果试图将i[0]索引为None,则会抛出错误。

由于某种原因,看起来您有一个包含单个元素的列表,这应该可以工作:
df_total['probability]=df_total['probability']。apply(lambda x:x[0])
谢谢Edchum,这很有效!!由于某种原因,看起来您有一个包含单个元素的列表,这应该可以工作:
df_total['probability]=df_total['probability']。apply(lambda x:x[0])
谢谢Edchum,这很有效!!