numpy

Creating array with single structured element containing an array

故事扮演 提交于 2021-01-28 07:51:32
问题 I have a dtype like this: >>> dt = np.dtype([('x', object, 3)]) >>> dt dtype([('x', 'O', (3,))]) One field named 'x', containing three pointers. I would like to construct an array with a single element of this type: >>> a = np.array([(['a', 'b', 'c'])], dtype=dt) >>> b = np.array([(np.array(['a', 'b', 'c'], dtype=object))], dtype=dt) >>> c = np.array((['a', 'b', 'c']), dtype=dt) >>> d = np.array(['a', 'b', 'c'], dtype=dt) >>> e = np.array([([['a', 'b', 'c']])], dtype=dt) All five of these

Choosing a box of data points from a plot

三世轮回 提交于 2021-01-28 07:42:47
问题 I have a catalogue contains three columns and I would like to read them in an array and exclude some of data-points from my catalogue by choosing them from two different plots. If I would call the columns of my catalogue 'm' , 'rh' , and 'rg' , I would like to exclude data-points by choosing different boxes in a 'm-rh' diagram and 'm-rg' plot. How should it be done? I came across this examples but it doesn't return any values like a numpy array ? Any help contains where should I start or how

Python import error “getfullargspec”

一曲冷凌霜 提交于 2021-01-28 07:34:59
问题 when I do: from sklearn import linear_model I get the error: ImportError: cannot import name 'getfullargspec' Interestingly, this does not happen a few days ago. So I tried to install python and scipy stack again on my computer using Anaconda again but does not solve the problem. What might be wrong with my system? Thank you in advance. 回答1: Install using pip pip install scipy And use pip to install the following packages such as numpy, pandas, etc... If you are using Python 3 then install

Pandas column of lists to separate columns

安稳与你 提交于 2021-01-28 07:03:26
问题 Problem Incoming data is a list of 0+ categories: #input data frame df = pd.DataFrame({'categories':(list('ABC'), list('BC'), list('A'))}) categories 0 [A, B, C] 1 [B, C] 2 [A] I would like to convert this to a DataFrame with one column per category and a 0/1 in each cell: #desired output A B C 0 1 1 1 1 0 1 1 2 1 0 0 Attempt OneHotEncoder with LabelEncoder get stuck because they don't handle lists in cells. The desired result is currently achieved with nested for loops: #get unique

How to get the count from a dataframe from a Dash Callback

此生再无相见时 提交于 2021-01-28 06:47:45
问题 I'd like to display the count of certain criteria inside a div in my dash layout based off callback selection from dropdown. I'm able to get the dropdown for the values in a pandas dataframe column, but I'm having trouble figuring out how to display the total count of the a selected element of the column. For example, I've written a function in Jupyter notebook to get a count def getCount(df, selected_org): totCount = df[df['ORGANIZATIONS'] == selected_org].AGENCY.count() return count

python split array into sub arrays of equivalent rank

≯℡__Kan透↙ 提交于 2021-01-28 06:33:25
问题 EDITED: I have an array of n sorted values. I want to create m sub-arrays so that my best element goes into my first sub-array, my second element goes into my second sub array, etc, and my n+1-th best element goes into my first sub array. If I have just two arrays its easy but if I want more than two sub-arrays I don't know how to do it. for example if I have an initial array: a = [50, 45, 40, 35, 30, 25, 20, 10, 9, 8] And I want 3 sub-arrays I should get: x1: [50, 35, 20, 8] x2: [45, 30, 10]

Converting Numpy Structured Array to Pandas Dataframes

僤鯓⒐⒋嵵緔 提交于 2021-01-28 06:24:58
问题 I obtain a structured numpy array from the following code: data = np.genfromtxt(fname, dtype = None, comments = '#', skip_header=1, usecols=(ucols)) where the first column is the indices of the rest of the data set in a scrambled order (which I wish to preserve). I would like to convert the structured array into a Pandas dataframe with the scrambled indices as the callable indices of the dataframe. EDIT: import numpy as np test = np.array([(45,1,'mars',1,1),(67,1,'pluto',1,1),(12,1,'saturn',1

How to use numpy.savetxt at the top of a file

喜夏-厌秋 提交于 2021-01-28 06:03:06
问题 My program writes several numpy arrays to a text file, then at the end I am trying to add the headers (another numpy array) to the top of the text file. I'm trying to either write over the first line (and just use a placeholder at the start of the file) or preferably insert the numpy array to the first line. The header function with numpy.savetxt won't work because it just writes the header over the last numpy array rather than the top of the file. I've also tried: Output.seek(0) np.savetxt

Efficient way of aggregating previous(in time) rows

╄→尐↘猪︶ㄣ 提交于 2021-01-28 06:02:39
问题 I have the following dataframe of orders placed by different customers, at different times: rng = list(pd.date_range('2019-02-24', periods=5, freq='T')) + list(pd.date_range('2019-03- 13', periods=2, freq='T')) + list(pd.date_range('2019-02-27', periods=1, freq='T')) customers = ["12987"]*5 + ["89563"]*2 + ["56733"] articles = ["8473", "7631", "1264", "8473", "5641", "9813", "7631", "1132"] order_history = pd.DataFrame({'Customer_no': customers, 'Date': rng, 'Article_no': articles}) order

Efficient way of aggregating previous(in time) rows

萝らか妹 提交于 2021-01-28 05:56:12
问题 I have the following dataframe of orders placed by different customers, at different times: rng = list(pd.date_range('2019-02-24', periods=5, freq='T')) + list(pd.date_range('2019-03- 13', periods=2, freq='T')) + list(pd.date_range('2019-02-27', periods=1, freq='T')) customers = ["12987"]*5 + ["89563"]*2 + ["56733"] articles = ["8473", "7631", "1264", "8473", "5641", "9813", "7631", "1132"] order_history = pd.DataFrame({'Customer_no': customers, 'Date': rng, 'Article_no': articles}) order