파이썬에서 키로 카운터 정렬
다음과 같은 카운터가 있습니다. Counter: {('A': 10), ('C':5), ('H':4)} counter.most_common () 이 아닌 알파벳 순서로 키를 정렬하고 싶습니다. 이것을 달성하는 방법이 있습니까? 해결 방법 >>> from collections import Counter >>> counter = Counter({'A': 10, 'C': 5, 'H': 7}) >>> counter.most_common() [('A', 10), ('H', 7), ('C', 5)] >>> sorted(counter.items()) [('A', 10), ('C', 5), ('H', 7)] 참조 페이지 https://stackoverflow.com/questions/17930814
2021. 1. 11.
파이썬 numpy.genfromtxt를 사용하여 쉼표가 포함 된 문자열이있는 csv 파일 읽기
numpy.genfromtxt 로 csv 파일을 읽으려고하는데 일부 필드는 쉼표가 포함 된 문자열입니다. 문자열은 따옴표 안에 있지만 numpy는 따옴표를 단일 문자열을 정의하는 것으로 인식하지 않습니다. 예를 들어 't.csv'의 데이터를 사용하는 경우 : 2012, "Louisville KY", 3.5 2011, "Lexington, KY", 4.0 코드 np.genfromtxt('t.csv', delimiter=',') 오류를 생성합니다. ValueError : 일부 오류가 감지되었습니다! 줄 # 2 (3 대신 4 개의 열이 있음) 내가 찾고있는 데이터 구조는 다음과 같습니다. array([['2012', 'Louisville KY', '3.5'], ['2011', 'Lexington, KY', '..
2021. 1. 11.
파이썬의 로그 Y 축 Bin
데이터 열의 히스토그램을 만들고 로그 ( y 축 )로 플로팅하려고하는데 다음 코드가 작동하지 않는 이유를 모르겠습니다. import numpy as np import matplotlib.pyplot as plt data = np.loadtxt('foo.bar') fig = plt.figure() ax = fig.add_subplot(111) plt.hist(data, bins=(23.0, 23.5,24.0,24.5,25.0,25.5,26.0,26.5,27.0,27.5,28.0)) ax.set_xlim(23.5, 28) ax.set_ylim(0, 30) ax.grid(True) plt.yscale('log') plt.show() 또한 plt.yscale ( 'log') 대신 plt.hist 줄에 Log =..
2021. 1. 11.