python 我有一个 Dataframe ,其中一列包含日期和时间,我想将每一天及其时间放在不同的列中

osh3o9ms  于 2022-12-02  发布在  Python
关注(0)|答案(2)|浏览(152)

我有一个 Dataframe ,其中一列包含日期和时间,我想把每一天和时间放在各自的列中。
我已经把一个'$'在每一天要么分裂或使用它把它在其各自的列。

import pandas as pd

data = [{'timings' : 'Friday 10 am - 6:30 pm$Saturday 10am-6:30pm$Sunday Closed$Monday 10am-6:30pm$Tuesday 10am-6:30pm$Wednesday 10am-6:30pm$Thursday 10am-6:30pm',
'monday':'','tuesday':'','wednesday':'','thursday':'','friday':'','saturday':'','sunday':''
}]

df = pd.DataFrame.from_dict(data)

例如:数据包含df ['timing'] =“星期五上午10点,星期六下午6:30”,则在df ['friday']中= '上午10点'和df ['sunday'] = '下午6:30'。
我不知道怎么用语言表达。
请我解决这个问题。

bfrts1fy

bfrts1fy1#

对字典列表使用嵌套列表解析,然后传递给DataFrame构造函数:

L = [dict(y.split(maxsplit=1) for y in x.split('$')) for x in df['timings']]

df = pd.DataFrame(L, index=df.index)
print (df)  
            Friday     Saturday  Sunday       Monday      Tuesday  \
0  10 am - 6:30 pm  10am-6:30pm  Closed  10am-6:30pm  10am-6:30pm   

     Wednesday     Thursday  
0  10am-6:30pm  10am-6:30pm
vvppvyoh

vvppvyoh2#

您可以使用str.extractall提取日期名称和时间,然后重新调整DataFrame的形状:

(df['timings'].str.extractall(r'(?P<day>[^$\s]+)\s+([^$]+)')
 .droplevel('match')
 .set_index('day', append=True)[1].unstack('day')
)

输出量:

day           Friday       Monday     Saturday  Sunday     Thursday      Tuesday    Wednesday
0    10 am - 6:30 pm  10am-6:30pm  10am-6:30pm  Closed  10am-6:30pm  10am-6:30pm  10am-6:30pm

如果要保持原始的日期顺序:

(df['timings'].str.extractall('(?P<day>[^$\s]+)\s+([^$]+)')
 .set_index('day', append=True)[1].unstack(['match', 'day'])
 .droplevel('match', axis=1)
)

输出量:

day           Friday     Saturday  Sunday       Monday      Tuesday    Wednesday     Thursday
0    10 am - 6:30 pm  10am-6:30pm  Closed  10am-6:30pm  10am-6:30pm  10am-6:30pm  10am-6:30pm

基于自定义顺序排序的替代方法(此处为星期五优先):

from calendar import day_name

sorter = pd.Series({d: (i+3)%7 for i,d in enumerate(day_name)})

out = (df['timings']
 .str.extractall('(?P<day>[^$\s]+)\s+([^$]+)')
 .droplevel('match')
 .set_index('day', append=True)[1].unstack('day')
 .sort_index(axis=1, key=sorter.get)
)

相关问题