得到一个包含几列的csv文件,并且数据包含某些列的空值。使用pandas dataframe函数,如何将包含空值和空列名的列总数打印到输出的csv文件中?
Output.csv 2#列数 列a b列
答案 0 :(得分:0)
尝试:
import UIKit
class ViewController: UIViewController {
private lazy var cameraService = CameraService()
private weak var button: UIButton?
private weak var imagePreviewView: UIImageView?
private var cameraInited = false
private enum ButtonState { case cancel, makeSnapshot }
private var buttonState = ButtonState.makeSnapshot {
didSet {
switch buttonState {
case .makeSnapshot: button?.setTitle("Make a photo", for: .normal)
case .cancel: button?.setTitle("Cancel", for: .normal)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
setupCameraPreviewView()
setupButton()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
cameraService.start()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
cameraService.stop()
}
// Ensure that the interface stays locked in Portrait.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
// Ensure that the interface stays locked in Portrait.
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
}
extension ViewController {
private func setupCameraPreviewView() {
let previewView = UIView(frame: .zero)
view.addSubview(previewView)
previewView.translatesAutoresizingMaskIntoConstraints = false
previewView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
previewView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
previewView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
previewView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
previewView.layoutIfNeeded()
cameraService.prepare(previewView: previewView, cameraPosition: .front) { [weak self] success in
if success { self?.cameraService.start() }
}
}
private func setupButton() {
let button = UIButton(frame: .zero)
button.addTarget(self, action: #selector(buttonTouchedUpInside), for: .touchUpInside)
view.addSubview(button)
self.button = button
buttonState = .makeSnapshot
button.translatesAutoresizingMaskIntoConstraints = false
button.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
button.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 44).isActive = true
button.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}
private func show(image: UIImage) {
let imageView = UIImageView(frame: .zero)
view.insertSubview(imageView, at: 1)
imagePreviewView = imageView
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
imageView.image = image
}
@objc func buttonTouchedUpInside() {
switch buttonState {
case .makeSnapshot:
cameraService.capturePhoto { [weak self] image in
guard let self = self else {return }
self.cameraService.stop()
self.buttonState = .cancel
self.show(image: image)
}
case .cancel:
buttonState = .makeSnapshot
cameraService.start()
imagePreviewView?.removeFromSuperview()
}
}
}
其中a是您的数据帧名称,并且要检查整个列是否为空,将any()更改为all()。
答案 1 :(得分:0)
以下代码可帮助您达到上述要求:
df=pd.DataFrame({'Name':["abc","def",None],'Age':[1,None,3],'Address':["rst","uvw","xyz"]})
null_colname=df.columns[df.isnull().any()].tolist() #find columns which returns True for null testing and convert the column names to list
null_colnum=len(null_colname) # take length of the above list
p=str(null_colnum)+"# of columns:" # initialize string in the format of required output
for i in range(0,null_colnum): #iterate over the list
p=p+'Column-'+null_colname[i]+' ' # concatenate column names to the string
text_file = open(filepath+"Output.csv", "w") #export to csv
text_file.write("%s" % p)
text_file.close()
答案 2 :(得分:0)
我准备了如下测试数据:
np.random.seed(0)
df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('ABCDEFGHIJ'))
df[df > 0.9] = pd.np.nan; df
要获取包含名称 NaN 的列名,请运行:
nn = df.isnull().any()
对于我的测试数据,结果是:
A True
B False
C False
D True
E False
F False
G False
H True
I True
J False
dtype: bool
我们实际上对索引值为 True 的索引值感兴趣。 要获取它们,请运行:
nullCols = nn.index[nn].tolist()
结果是:
['A', 'D', 'H', 'I']
要获取此类列的数量,请运行:
len(nullCols)
结果为4
。