简体   繁体   English

如何将值拆分为列中的 int 和 string 并计算持续时间

[英]How can I split values as int and string in column and calculate duration

In my dataframe, there are several columns.在我的 dataframe 中有几列。 One of them is based on time duration.其中之一是基于持续时间。 While some of them is based on hours and others are based on minutes.其中一些基于小时,而另一些基于分钟。

Like喜欢

2.5 hours
1 hours
41 minutes
42 hours
...

The column info is here栏目信息在这里

content_duration     3683 non-null   object

I want to convert all datas as minute values and their column are based on float or int type.我想将所有数据转换为分钟值,并且它们的列基于 float 或 int 类型。

I wrote a function to perform this process but there is a problem to convert string to int.我写了一个 function 来执行这个过程,但是将字符串转换为 int 时出现问题。

How can I fix the issue?我该如何解决这个问题?

Here is my code snippet shown below.这是我的代码片段,如下所示。

def convertminutes(column):
    value = column.str.split(' ').str[0].astype(float)
    timetype = column.str.split(' ').str[1]
    for i in range(len(timetype)):
        if timetype[i] == "hours" or timetype[i] == "hour":
            value[i] = value[i] * 60
        else:
            value[i] = value[i]        
    return value 

I Called the function in this code shown below.我在下面显示的代码中调用了 function。

df["content_duration"] = convertminutes(df["content_duration"])

Here is the screenshot这是屏幕截图

在此处输入图像描述

Try this:尝试这个:

import pandas as pd

def convert_to_minutes(x):
    return float(x.split()[0]) * 60 if 'hour' in x else float(x.split()[0])

df = pd.DataFrame({'content_duration': ['1 hour', '2.5 hours', '18 minutes','3 hours', '12 hours', '16 minutes', ]})
df['content_duration'] = df['content_duration'].apply(convert_to_minutes)
print(df)

Output: Output:

   content_duration
0              60.0
1             150.0
2              18.0
3             180.0
4             720.0
5              16.0

Here's my attempt:这是我的尝试:

def convertminutes(column):
    map_dict = {"hours": 60, "hour": 60, "minute": 1, "minutes": 1}
    expanded_df = column.str.split(' ', expand=True)
    expanded_df[1] = copy_df[1].map(map_dict)
    return expanded_df[0].astype(float) * expanded_df[1]

Not the most efficient route, but it works.不是最有效的路线,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM