我需要数据帧,我用的是熊猫。 我想从一个可变日期到一列中的值做一个累计和
我想在df2中添加第二列来显示日期,以便知道df2中date2之后AVG列的和大于100的日期。在
例如,df1和df2是我开始的数据帧,df3是我想要的,df3['date100']是平均值总和大于100的那一天:
df1 = pd.DataFrame({'date1': ['1/1/2014', '2/1/2014', '3/1/2014','1/1/2014', '2/1/2014', '3/1/2014','1/1/2014', '2/1/2014', '3/1/2014'],
'Place':['A','A','A','B','B','B','C','C','C'],'AVG': [62,14,47,25,74,60,78,27,41]})
df2 = pd.DataFrame({'date2': ['1/1/2014', '2/1/2014'], 'Place':['A','C'])})
*Something*
df3 = pd.DataFrame({'date2': ['1/1/2014', '2/1/2014'], 'Place':['A','C'], 'date100': ['3/1/2014', '2/1/2014'], 'sum': [123, 105]})
我找到了一些答案,但大多数都使用groupby,而df2没有分组。在
因为你的例子非常基础,如果你有边缘案例,你想让我处理,只要问。此解决方案意味着:
解决方案:
这是一个直接的解决方案,假设如下:
df1
按日期排序df2
中的每个日期都存在一个解决方案然后可以执行以下操作:
这将执行以下操作:
df2
中的每个日期,找到第一个平均值至少为100的日期df1
中该行的索引索引索引的单个数据帧中ix
列中,并重置该索引以将该数据帧连接到df2ix
列将其连接到df1减去AVG
列ix
列,然后重新排列所有内容相关问题 更多 >
编程相关推荐