indexing

Filling the missing index and filling its value with 0

限于喜欢 提交于 2020-04-28 16:48:28
问题 I have a pandas dataframe with a column value as index number Sales 140 100 142 200 145 300 I want to fill the missing index and also want to fill the value of missing index with 0 Sales 140 100 141 0 142 200 143 0 144 0 145 300 I also want to fill missing values as the missing index number like Week_num 140 140 142 142 145 145 Week_Num 140 140 141 141 142 142 143 143 144 144 145 145 I request you to help me how to code this out? 回答1: You can using reindex df.reindex(list(range(df.index.min()

How to efficiently sort by the results of a subquery?

北城余情 提交于 2020-04-17 22:10:25
问题 Let's say I have a site like Stackoverflow with posts that people can reply to, and I would like to have an interface for sorting the posts by reply count This will eventually have infinite scroll pagination, so showing 10 results at a time. Here's an example query for that: SELECT *, (SELECT COUNT(*) FROM post_reply pr WHERE pr.convo_id = post.convo_id) as replies FROM post ORDER BY replies LIMIT 10; This works, but it is prohibitively slow. I have hundreds of thousands of posts and this

Pandas read_excel sometimes creates index even when index_col=None

我怕爱的太早我们不能终老 提交于 2020-04-13 16:54:08
问题 I'm trying to read an excel file into a data frame and I want set the index later, so I don't want pandas to use column 0 for the index values. By default ( index_col=None ), it shouldn't use column 0 for the index but I find that if there is no value in cell A1 of the worksheet it will. Is there any way to over-ride this behaviour (I am loading many sheets that have no value in cell A1)? This works as expected when test1.xlsx has the value "DATE" in cell A1: In [19]: pd.read_excel('test1

Indexing API 403 Permission denied. Failed to verify the URL ownership

穿精又带淫゛_ 提交于 2020-04-13 16:49:19
问题 I want to execute the code from the example: require_once 'google-api-php-client/vendor/autoload.php'; $client = new Google_Client(); // service_account_file.json is the private key that you created for your service account. $client->setAuthConfig('service_account_file.json'); $client->addScope('https://www.googleapis.com/auth/indexing'); // Get a Guzzle HTTP Client $httpClient = $client->authorize(); $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish'; // Define

Pandas assign the groupby sum value to the last row in the original table

久未见 提交于 2020-04-13 06:47:23
问题 For example, I have a table A id price sum 1 2 0 1 6 0 1 4 0 2 2 0 2 10 0 2 1 0 2 5 0 3 1 0 3 5 0 What I want is like (the last row of sum should be the sum of price of a group) id price sum 1 2 0 1 6 0 1 4 12 2 2 0 2 10 0 2 1 0 2 5 18 3 1 0 3 5 6 What I can do is find out the sum using A['price'].groupby(A['id']).transform('sum') However I don't know how to assign this to the sum column (last row). Thanks 回答1: Use last_valid_index to locate rows to fill g = df.groupby('id') l = pd.DataFrame

Avoiding sub2ind and ind2sub

末鹿安然 提交于 2020-04-10 05:03:29
问题 I need to access several indices around a certain point in 3D. For example, for point ( x1 , y1 , z1 ) I need to get all the indices of its 3x3x3 neighborhood such that ( x1 , y1 , z1 ) is centered. For neighborhood of size 3, I do it with [x,y,z] = meshgrid(-1:1,-1:1,-1:1); x_neighbors = bsxfun(@plus,x,x1); y_neighbors = bsxfun(@plus,y,y1); z_neighbors = bsxfun(@plus,z,z1); Here, I center x1 , y1 , z1 to (0,0,0) by adding the distances from ( x1 , y1 , z1 ) to any point in the 3x3x3 box.

In numpy, what does indexing an array with the empty tuple vs. ellipsis do?

China☆狼群 提交于 2020-04-08 02:04:32
问题 I just discovered — by chance — that an array in numpy may be indexed by an empty tuple: In [62]: a = arange(5) In [63]: a[()] Out[63]: array([0, 1, 2, 3, 4]) I found some documentation on the numpy wiki ZeroRankArray: (Sasha) First, whatever choice is made for x[...] and x[()] they should be the same because ... is just syntactic sugar for "as many : as necessary", which in the case of zero rank leads to ... = (:,)*0 = (). Second, rank zero arrays and numpy scalar types are interchangeable

In numpy, what does indexing an array with the empty tuple vs. ellipsis do?

 ̄綄美尐妖づ 提交于 2020-04-08 02:04:13
问题 I just discovered — by chance — that an array in numpy may be indexed by an empty tuple: In [62]: a = arange(5) In [63]: a[()] Out[63]: array([0, 1, 2, 3, 4]) I found some documentation on the numpy wiki ZeroRankArray: (Sasha) First, whatever choice is made for x[...] and x[()] they should be the same because ... is just syntactic sugar for "as many : as necessary", which in the case of zero rank leads to ... = (:,)*0 = (). Second, rank zero arrays and numpy scalar types are interchangeable

Rebuild all indexes in a Database

廉价感情. 提交于 2020-04-07 10:58:12
问题 I have a very large SQL Server 2008 R2 database (1.5TB) and will be copying some data from column to column within the same table. I've been told that the schema has a large number of indexes and was wondering if there is a default query or script that will rebuild all the indexes. Have also been advised to update the statistics at the same time? Each of the 30 tables has one clustered index and 13x non-clustered indexes Thanks. 回答1: Try the following script: Exec sp_msforeachtable 'SET

Pandas- Select rows from DataFrame based on condition

*爱你&永不变心* 提交于 2020-04-07 06:53:01
问题 DataFrame : category value A 25 B 10 A 15 B 28 A 18 Need to Select rows where following conditions are satisfied, 1. category=A and value between 10 and 20 2. categories other than A 回答1: I think you need boolean indexing: df1 = df[(df['category'] == 'A') & (df['value'].between(10,20))] print (df1) category value 2 A 15 4 A 18 And then: df2 = df[(df['category'] != 'A') & (df['value'].between(10,20))] print (df2) category value 1 B 10 Or: df3 = df[df['category'] != 'A'] print (df3) category