Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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_Excel_Pandas - Fatal编程技术网

Python 如何将列中的文本拆分为多行?

Python 如何将列中的文本拆分为多行?,python,excel,pandas,Python,Excel,Pandas,我正在处理一个excel文件,我想将一列中包含许多时间记录器的内容拆分出来。我想知道如何使用pandas或python来实现这一点? 警告:时间列不是固定模式 我想要的是这样 假设您的时间列在小时/分钟之前的0小于10。您可以使用将其平均分割为块 导入文本包装 times_list=df['time'].applylambda x:textwrap.wrapx,5.tolist time_df=pd.DataFrame[时间列表中值的pd.Seriesvalue] 您可以这样做: from op

我正在处理一个excel文件,我想将一列中包含许多时间记录器的内容拆分出来。我想知道如何使用pandas或python来实现这一点? 警告:时间列不是固定模式

我想要的是这样

假设您的时间列在小时/分钟之前的0小于10。您可以使用将其平均分割为块

导入文本包装 times_list=df['time'].applylambda x:textwrap.wrapx,5.tolist time_df=pd.DataFrame[时间列表中值的pd.Seriesvalue]
您可以这样做:

from openpyxl import *
wb = load_workbook('Excel path')

# grab the active worksheet
ws = wb.active

for index in range(1, 11):
    num_times = int(len(ws['A{index}'.format(index=index)].value)/5)
    for t in range(1, num_times+1):
        i_str = 5*t
        ws.cell(row=index, column=t+1, value=ws['A{index}'.format(index=index)].value[i_str-5:i_str])

# Save the file
wb.save('Excel path')
输出:


有了你们展示的样品,你们可以试试下面的吗

import pandas as pd
df=pd.DataFrame({'time':['07:2507:3007:57:21:39','07:1817:2517:5521:23','07:2018:35']})
pd.DataFrame(list(df['time'].str.findall(r'\d{2}:\d{2}')))
输出如下:

       0      1      2      3
0  07:25  07:30  07:57  21:39
1  07:18  17:25  17:55  21:23
2  07:20  18:35   None   None
       0      1      2      3
0  07:25  07:30  07:57  21:39
1  07:18  17:25  17:55  21:23
2  07:20  18:35   None   None