python - After using groupby, how do I get all values from multiple rows into a list?
问题描述
I have a DataFrame with names of people, dates, start/end times, and durations. I want to group by name and date, sum the Duration, and also "sum" the Start and End values by throwing them into a list.
df = pd.DataFrame([
['Bar', '2/18/2019', '7AM', '9AM',120],
['Bar', '2/18/2019', '9AM', '11AM',120],
['Foo', '2/18/2019', '10AM', '12PM',120],
],
columns=['Name', 'Date', 'Start','End','Duration'])
Looking to turn this...
Into this...
Where I am using groupby to get the sum of Duration for Name and Date...
df.groupby(['Name','Date'])['Duration'].sum().reset_index()
...but having a heck of a time trying to figure out how to throw all of those times into a list. I've tried .apply and building a dictionary where the key is Name+date and the value is the list, but to no avail.
Any hints or gentle nudges in the right direction?
解决方案
尝试这个:
df['Time'] = df['Start'] + '-' + df['End']
df.groupby(['Name', 'Date']).apply(lambda x: pd.Series({
'Duration': x['Duration'].sum(),
'Times': x['Time'].values
}))
Times
现在包含ndarray
s 个字符串。
推荐阅读
- bash - 如何在 FOR 循环中正确处理 *(星号)?
- c++ - 如何强制 getline() 一次输入一行
- angular - 从 JSON 文件传递单个对象
- selenium - Selenium C# 成功运行测试,但在最后一次测试完成后,最后一次测试的浏览器保持打开状态
- bash - 为什么使用 || 而不是bash中的IF中的-o?
- python - 无法在 Python 中加载 HDF5 文件
- python - 通过 pandas groupby 设置多级函数
- ssas - SSAS表格模型:更改字符串值的情况不起作用
- reactjs - mapStateToProps 后 Props 仍然为空
- javascript - 如何从由句号、升级或问号结尾的句子组成的文本中忽略 .com .net 或 .org 成句子数组?