我正在尝试根据数据帧中另一列的特定值向列添加增量值。这样...
import UIKit
import SDWebImage
class testGraphic2ViewController: UIViewController, SDWebImageManagerDelegate {
@IBOutlet weak var imageView: UIImageView!
let postImageUrlStr = "https://firebasestorage.googleapis.com/v0/b/instagramclone-be6eb.appspot.com/o/posts%2FFBAE2BE9-F429-4D39-978C-DD2E24BF062D?alt=media&token=7b41569b-5451-4728-942c-6d43ec50350c"
override func viewDidLoad() {
super.viewDidLoad()
SDWebImageManager.shared().delegate = self
SDWebImageManager.shared().loadImage(with: URL(string: postImageUrlStr), options: [], progress: nil) { (image, data, error, cacheType, finished, url) in
self.imageView.image = image
}
}
func imageManager(_ imageManager: SDWebImageManager, transformDownloadedImage image: UIImage?, with imageURL: URL?) -> UIImage? {
print("imageManager is called!")
guard let image = image, let imageURL = imageURL else {
return nil
}
if (imageURL.lastPathComponent == "large" && (image.size.width > 1000 || image.size.width > 1000)) {
let targetSize = CGSize(width: 1000, height: 1000)
UIGraphicsBeginImageContextWithOptions(targetSize, false, image.scale)
image.draw(in: CGRect(origin: .zero, size: targetSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
} else {
return image
}
}
会变成这样:
col A col B
A 0
B 1
C 2
A 3
A 4
B 5
曾尝试使用groupby函数,但无法真正了解设置B列的增量值。
有什么想法吗?
谢谢
答案 0 :(得分:2)
我认为需要factorize
:
df['col B'] = pd.factorize(df['col A'])[0] + 1
print (df)
col A col B
0 A 1
1 B 2
2 C 3
3 A 1
4 A 1
5 B 2
另一种解决方案:
df['col B'] = pd.Categorical(df['col A']).codes + 1
print (df)
col A col B
0 A 1
1 B 2
2 C 3
3 A 1
4 A 1
5 B 2