我有一个行名相同的数据集。例如,我想在数据(第0,1,2,3,4行)中使用与A的最后一行(第4行)相同的价格。所以所有的As都等于60.但是我希望这对我的整个数据都是如此。 B和C等的计算相同。我有很多数据。是不是可以在熊猫中做到这一点?我不想将它们分组,我想保留所有As等。数据集的一个例子如下。
Brand Price Color Category
0 A 10 Blue Shoes
1 A 20 Red Shoes
2 A 30 Yellow Shoes
3 A 40 Pink Shoes
4 A 60 Purple Shoes
5 B 100 Red Shoes
6 B 130 Green Shoes
7 B 150 Blue Shoes
8 C 170 Yellow Shoes
9 C 20 Green Shoes
谢谢
答案 0 :(得分:1)
使用map
创建的Series
keep='last'
与mapping = df.drop_duplicates('Brand', keep='last').set_index('Brand')['Price']
df['Price'] = df['Brand'].map(mapping)
print (df)
Brand Price Color Category
0 A 60 Blue Shoes
1 A 60 Red Shoes
2 A 60 Yellow Shoes
3 A 60 Pink Shoes
4 A 60 Purple Shoes
5 B 150 Red Shoes
6 B 150 Green Shoes
7 B 150 Blue Shoes
8 C 20 Yellow Shoes
9 C 20 Green Shoes
一起使用每组的最新值:
Brand
如果需要按多列定义分组(例如Category
和df['Price'] = df.groupby(['Brand', 'Category'])['Price'].transform('last')
print (df)
Brand Price Color Category
0 A 60 Blue Shoes
1 A 60 Red Shoes
2 A 60 Yellow Shoes
3 A 60 Pink Shoes
4 A 60 Purple Shoes
5 B 150 Red Shoes
6 B 150 Green Shoes
7 B 150 Blue Shoes
8 C 20 Yellow Shoes
9 C 20 Green Shoes
),可以drop_duplicates
使用transform
:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
if (application.applicationState == UIApplicationStateActive) {
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}