嗨,我有一个像下面这样的pandas Dataframe 列是['music_id','users_id','得分']
@interface NSString (SHA2HEX)
/*
Get the SHA2 (256 bit) digest as a hex string.
*/
@property (nonatomic, readonly) NSString* sha2hex;
@end
@implementation NSString (SHA2HEX)
- (NSString*)sha2hex
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
if (data.length > UINT32_MAX)
return nil;
uint8_t digest[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
const int hexlen = CC_SHA256_DIGEST_LENGTH * 2;
NSMutableString *hexstr = [NSMutableString stringWithCapacity:hexlen];
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
[hexstr appendFormat:@"%02x", digest[i]];
}
return hexstr;
}
@end
我尝试制作一个像这样的新数据框:
A a 1
A c 2
A b 1
B e 2
B a 3
B b 1
pandas中有没有像这样的方法? 谢谢!
答案 0 :(得分:1)
使用set_index
和unstack
:
df.set_index([0,1])[2].unstack()
输出:
1 a b c e
0
A 1.0 1.0 2.0 NaN
B 3.0 1.0 NaN 2.0
摆脱轴名称输出:
df.set_index([0,1])[2].unstack().rename_axis(None, axis=1).rename_axis(None)
a b c e
A 1.0 1.0 2.0 NaN
B 3.0 1.0 NaN 2.0
其中,df:
0 1 2
0 A a 1
1 A c 2
2 A b 1
3 B e 2
4 B a 3
5 B b 1