我想知道如何最好地在数据帧中有条件地拆分字符串,以便正确的值到达正确的列。当字符串的长度变化时,请考虑以下内容。时间以小时,分钟,秒为单位。
time = ['22:35', '1:20:20']
df = pd.DataFrame({'time': time})
df
time
0 22:35
1 1:20:20
对此进行了尝试:
for i in df['time']:
if len(i) == 5:
df[['minutes', 'seconds']] = df['time'].str.split(':', expand=True)
if len(i) == 7:
df[['hours', 'minutes', 'seconds']] = df['time'].str.split(':', expand=True)
答案 0 :(得分:0)
可能不是一个很好的答案,但我相信这是可行的:
time = ['22:35', '1:20:20']
templist = [item.split(":") for item in time]
templist2 = []
for item in templist:
if len(item) == 3:
thisdict = {
"hours": item[0],
"minutes": item[1],
"seconds": item[2]
}
templist2.append(thisdict)
elif len(item) == 2:
thisdict = {
"hours": "0",
"minutes": item[0],
"seconds": item[1]
}
templist2.append(thisdict)
elif len(item) == 1:
thisdict = {
"hours": "0",
"minutes": "0",
"seconds": item[0]
}
templist2.append(thisdict)
df = pd.DataFrame(templist2)
答案 1 :(得分:0)
我同意@Mark Wang和@ s.k,但这可能对您有用:
import datetime
def time_converter(s):
nb_semicolons = s.count(':')
if nb_semicolons == 1:
form = '%M:%S'
if nb_semicolons == 2:
form = '%H:%M:%S'
return datetime.datetime.strptime(s, form).time()
df['time2'] = df['time'].apply(time_converter)
答案 2 :(得分:0)
事实证明这很容易:
import pandas as pd
time = ['22:35', '1:20:20']
df = pd.DataFrame({'time': time})
def f1(x):
if len(x) == 5:
x = '0:' + x
return x
df['time'] = df['time'].apply(f1)
df[['hours', 'minutes', 'seconds']] = df['time'].str.split(':', expand=True)
df
time hours minutes seconds
0 0:22:35 0 22 35
1 1:20:20 1 20 20