Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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中从数据列中提取两个数字?_Python_Python 3.x_Pandas_Dataframe_Data Cleaning - Fatal编程技术网

如何在python中从数据列中提取两个数字?

如何在python中从数据列中提取两个数字?,python,python-3.x,pandas,dataframe,data-cleaning,Python,Python 3.x,Pandas,Dataframe,Data Cleaning,我的数据集中有一个列,其中包含有声书籍的收听时间。数据的存储方式如下所示 10小时43分钟 如何在python数据帧中提取它们并将其更改为分钟 我用过 audiob_adv['time']=audiob_adv['Listening time'].str.extract('(\d\d) 但这并不正确 您需要更改所使用的正则表达式。您需要选择一个或多个数字\d+,然后您有一个或多个字母不是您不想选择的数字[^\d]+,并且您想选择分钟数的数字\d+。您需要将列类型更改为int32才能进行计算。使用

我的数据集中有一个列,其中包含有声书籍的收听时间。数据的存储方式如下所示 10小时43分钟

如何在python数据帧中提取它们并将其更改为分钟

我用过
audiob_adv['time']=audiob_adv['Listening time'].str.extract('(\d\d)

但这并不正确


您需要更改所使用的正则表达式。您需要选择一个或多个数字
\d+
,然后您有一个或多个字母不是您不想选择的数字
[^\d]+
,并且您想选择分钟数的数字
\d+
。您需要将列类型更改为int32才能进行计算。使用以下代码,您将得到您想要的:

temp_df = audiob_adv['Listening Time'].str.extract(r'(\d+)[^\d]+(\d+)').astype('int32')
audiob_adv["Time"] = temp_df.iloc[:,0]*60 + temp_df.iloc[:,1]

我试图实现,它说“不能将浮点NaN转换为整数”。我已经删除了所有的空值,