可复制示例:
df = pd.DataFrame([[1, '2015-12-15', 10],
[1, '2015-12-16', 13],
[1, '2015-12-17', 16],
[2, '2015-12-15', 19],
[2, '2015-12-11', 22],
[2, '2015-12-18', 25],
[3, '2015-12-14', 28],
[3, '2015-12-12', 31],
[3, '2015-12-15', 34]])
df.columns = ['X', 'Y', 'Z']
print(df.dtypes)
print()
print(df)
可复制示例的输出和每个列的数据类型:
X int64
Y object
Z int64
dtype: object
X Y Z
0 1 2015-12-15 10
1 1 2015-12-16 13
2 1 2015-12-17 16
3 2 2015-12-15 19
4 2 2015-12-11 22
5 2 2015-12-18 25
6 3 2015-12-14 28
7 3 2015-12-12 31
8 3 2015-12-15 34
预期产量:
X Y Z
0 1 2015-12-15 10
1 1 2015-12-15 10
2 2 2015-12-11 22
3 2 2015-12-15 19
4 3 2015-12-12 31
5 3 2015-12-15 34
解释输出是什么:
对于列中的每个组
X
分组后
十
,我想要一行的值在列中
Z
其中列中的值
Y
因为那一组是
min(all dates/object in column Y)
同样地
组,另一行的值在列“Z”中,其中值在列中
是的
因为那一组是
some custom date that definitely exists for all groups which will be hardcoded
. 所以每组都有两排。
在我的输出中,对于组
1
,列中的值
Z轴
是
10
,因为列中的值
Z轴
与
列中所有日期的最小值
是的
对于组
一
,
12-15-2015
是
十
. 对于同一组
一
,此组的第二行
一
,列中的值
Z轴
对于自定义日期
2015年12月15日
也是
十
. 对于组
2
,
min(all dates/objects in column Y)
是
2015-12-11
,列中的相应值
Z轴
对于组
二
列中有值
是的
,
2015年12月11日
是
22
. 以及定制日期
2015年12月15日
,它是
19
.
以下是我为完成此任务而编写的一些线性时间搜索/延迟代码:
uniqueXs = list(dict(Counter(df['X'].tolist())).keys()) #Get every unique item in column X is a list.
df_list = [] #Empty list that will have rows of my final DataFrame
for x in uniqueXs: #Iterate through each unique value in column X
idfiltered_dataframe = df.loc[df['X'] == x] #Filter DataFrame based on the current value in column X
#(iterating through list of all values)
min_date = min(idfiltered_dataframe['Y']) #Min of column Y
custom_date = '2015-12-15' #Every group WILL have this custom date.
mindatefiltered_dataframe = idfiltered_dataframe.loc[idfiltered_dataframe['Y'] == min_date] #Within group, filter rows where column Y has minimum date
customdatefiltered_dataframe = idfiltered_dataframe.loc[idfiltered_dataframe['Y'] == custom_date] #Within group, filter rows where column Y has a custom date
for row_1 in mindatefiltered_dataframe.index: #Iterate through mindatefiltered DataFrame and create list of each row value required
row_list = [mindatefiltered_dataframe.at[row_1, 'X'], mindatefiltered_dataframe.at[row_1, 'Y'], mindatefiltered_dataframe.at[row_1, 'Z']]
df_list.append(row_list) #Append to a master list
for row_2 in customdatefiltered_dataframe.index: #Iterate through customdatefiltered DataFrame and create list of each row value required
row_list = [customdatefiltered_dataframe.at[row_2, 'X'], customdatefiltered_dataframe.at[row_2, 'Y'], customdatefiltered_dataframe.at[row_2, 'Z']]
df_list.append(row_list) #Append to a master list
print(pd.DataFrame(df_list)) #Create DataFrame out of the master list
我的印象是有一些巧妙的方法,你只是
df.groupby..
得到预期的输出,我希望有人能给我提供这样的代码。