Questions tagged [indexing]
Indexing data structures is a general technique to improve the speed of data lookups.
indexing
34,432
questions
4415
votes
46
answers
6.4m
views
How to find the index for a given item in a list?
Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index 1?
2888
votes
7
answers
1.1m
views
How does database indexing work? [closed]
Given that indexing is so important as your data set increases in size, can someone explain how indexing works at a database-agnostic level?
For information on queries to index a field, check out How ...
2766
votes
26
answers
3.2m
views
How do I get the last element of a list?
How do I get the last element of a list? Which way is preferred?
alist[-1]
alist[len(alist) - 1]
2291
votes
18
answers
4.2m
views
How to remove an element from a list by index
How do I remove an element from a list by index?
I found list.remove(), but this slowly scans the list for an item by value.
1761
votes
22
answers
4.1m
views
Selecting multiple columns in a Pandas dataframe
How do I select columns a and b from df, and save them into a new dataframe df1?
index a b c
1 2 3 4
2 3 4 5
Unsuccessful attempt:
df1 = df['a':'b']
df1 = df.ix[:, 'a':'b']
1647
votes
43
answers
2.5m
views
How to change the order of DataFrame columns?
I have the following DataFrame (df):
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10, 5))
I add more column(s) by assignment:
df['mean'] = df.mean(1)
How can I move the ...
1452
votes
17
answers
2.2m
views
How to drop rows of Pandas DataFrame whose value in a certain column is NaN
I have this DataFrame and want only the records whose EPS column is not NaN:
STK_ID EPS cash
STK_ID RPT_Date
601166 20111231 601166 NaN NaN
600036 20111231 ...
1298
votes
12
answers
903k
views
What do Clustered and Non-Clustered index actually mean?
I have a limited exposure to DB and have only used DB as an application programmer. I want to know about Clustered and Non clustered indexes.
I googled and what I found was :
A clustered index is a ...
974
votes
7
answers
823k
views
How are iloc and loc different?
Can someone explain how these two methods of slicing are different? I've seen the docs
and I've seen previous similar questions (1, 2), but I still find myself unable to understand how they are ...
870
votes
5
answers
443k
views
Multiple Indexes vs Multi-Column Indexes
What is the difference between creating one index across multiple columns versus creating multiple indexes, one per column?
Are there reasons why one should be used over the other?
For example:
Create ...
840
votes
10
answers
1.3m
views
How to convert index of a pandas dataframe into a column
How to convert an index of a dataframe into a column?
For example:
gi ptt_loc
0 384444683 593
1 384444684 594
2 384444686 596
to
index1 gi ptt_loc
...
750
votes
6
answers
840k
views
How to avoid pandas creating an index in a saved csv
I am trying to save a csv to a folder after making some edits to the file.
Every time I use pd.to_csv('C:/Path of file.csv') the csv file has a separate column of indexes. I want to avoid printing ...
675
votes
10
answers
1.2m
views
How do I access the ith column of a NumPy multidimensional array?
Given:
test = np.array([[1, 2], [3, 4], [5, 6]])
test[i] gives the ith row (e.g. [1, 2]). How do I access the ith column? (e.g. [1, 3, 5]). Also, would this be an expensive operation?
670
votes
3
answers
367k
views
Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?
What are the differences between PRIMARY, UNIQUE, INDEX and FULLTEXT when creating MySQL tables?
How would I use them?
604
votes
14
answers
648k
views
How to see indexes for a database or table in MySQL?
How do I see if my database has any indexes on it?
How about for a specific table?
580
votes
8
answers
1.3m
views
Selecting a row of pandas series/dataframe by integer index
I am curious as to why df[2] is not supported, while df.ix[2] and df[2:3] both work.
In [26]: df.ix[2]
Out[26]:
A 1.027680
B 1.514210
C -1.466963
D -0.162339
Name: 2000-01-03 00:00:00
In ...
575
votes
4
answers
1.1m
views
How to reset index in a pandas dataframe?
I have a dataframe from which I remove some rows. As a result, I get a dataframe in which index is something like [1,5,6,10,11] and I would like to reset it to [0,1,2,3,4]. How can I do it?
The ...
550
votes
8
answers
1.9m
views
Python Pandas: Get index of rows where column matches certain value
Given a DataFrame with a column "BoolCol", we want to find the indexes of the DataFrame in which the values for "BoolCol" == True
I currently have the iterating way to do it, which ...
547
votes
8
answers
314k
views
Why use the INCLUDE clause when creating an index?
While studying for the 70-433 exam I noticed you can create a covering index in one of the following two ways.
CREATE INDEX idx1 ON MyTable (Col1, Col2, Col3)
-- OR --
CREATE INDEX idx1 ON MyTable (...
537
votes
10
answers
1.3m
views
Pandas: Get first row value of a given column
This seems like a ridiculously easy question... but I'm not seeing the easy answer I was expecting.
So, how do I get the value at an nth row of a given column in Pandas? (I am particularly interested ...
524
votes
13
answers
774k
views
What is an index in SQL?
Also, when is it appropriate to use one?
517
votes
8
answers
785k
views
How do I add indexes to MySQL tables?
I've got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run
SELECT * FROM table WHERE id = '1';
the code runs fine as the ID field is the primary index.
However, ...
505
votes
10
answers
297k
views
How to map/collect with index in Ruby?
What is the easiest way to convert
[x1, x2, x3, ... , xN]
to
[[x1, 2], [x2, 3], [x3, 4], ... , [xN, N+1]]
489
votes
9
answers
225k
views
Postgres and indexes on foreign keys and primary keys
Does Postgres automatically put indexes on foreign keys and primary keys? How can I tell? Is there a command that will return all indexes on a table?
479
votes
9
answers
142k
views
How do I handle too long index names in a Ruby on Rails ActiveRecord migration?
I am trying to add a unique index that gets created from the foreign keys of four associated tables:
add_index :studies,
["user_id", "university_id", "subject_name_id", ...
479
votes
9
answers
1.2m
views
Replace a character at a specific index in a string?
I'm trying to replace a character at a specific index in a string.
What I'm doing is:
String myName = "domanokz";
myName.charAt(4) = 'x';
This gives an error. Is there any method to do this?
477
votes
13
answers
417k
views
SQL join: selecting the last records in a one-to-many relationship
Suppose I have a table of customers and a table of purchases. Each purchase belongs to one customer. I want to get a list of all customers along with their last purchase in one SELECT statement. What ...
443
votes
10
answers
225k
views
How do MySQL indexes work?
I am really interested in how MySQL indexes work, more specifically, how can they return the data requested without scanning the entire table?
It's off-topic, I know, but if there is someone who ...
441
votes
4
answers
812k
views
Is there an R function for finding the index of an element in a vector?
In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively ...
429
votes
30
answers
885k
views
List of all index & index columns in SQL Server DB
How do I get a list of all index & index columns in SQL Server 2005+? The closest I could get is:
select s.name, t.name, i.name, c.name from sys.tables t
inner join sys.schemas s on t.schema_id = ...
409
votes
6
answers
602k
views
how do I insert a column at a specific column index in pandas?
Can I insert a column at a specific column index in pandas?
import pandas as pd
df = pd.DataFrame({'l':['a','b','c','d'], 'v':[1,2,1,2]})
df['n'] = 0
This will put column n as the last column of df,...
404
votes
10
answers
705k
views
Get column index from column name in python pandas
In R when you need to retrieve a column index based on the name of the column you could do
idx <- which(names(my_data)==my_colum_name)
Is there a way to do the same with pandas dataframes?
402
votes
9
answers
82k
views
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
For example, if I want to read the middle value from magic(5), I can do so like this:
M = magic(5);
value = M(3,3);
to get value == 13. I'd like to be able to do something like one of these:
value =...
367
votes
20
answers
695k
views
How can I remove an element from a list?
I have a list and I want to remove a single element from it. How can I do this?
I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't ...
342
votes
11
answers
435k
views
Access multiple elements of list knowing their index [duplicate]
I need to choose some elements from the given list, knowing their index. Let say I would like to create a new list, which contains element with index 1, 2, 5, from given list [-2, 1, 5, 3, 8, 5, 6]. ...
322
votes
8
answers
118k
views
Does MySQL index foreign key columns automatically?
Does MySQL index foreign key columns automatically?
319
votes
6
answers
118k
views
Disable IntelliJ indexing on specific folder
In my project, I have a .deploy folder which is created/updated when I deploy my app locally. Is it possible to disable indexing on that folder? Everything slows down whenever I deploy and it's really ...
309
votes
13
answers
304k
views
What are the differences between a clustered and a non-clustered index?
What are the differences between a clustered and a non-clustered index?
306
votes
18
answers
396k
views
How to get indices of a sorted array in Python
I have a numerical list:
myList = [1, 2, 3, 100, 5]
Now if I sort this list to obtain [1, 2, 3, 5, 100].
What I want is the indices of the elements from the
original list in the sorted order i.e. [...
298
votes
9
answers
245k
views
Difference between Key, Primary Key, Unique Key and Index in MySQL
When should I use KEY, PRIMARY KEY, UNIQUE KEY and INDEX?
285
votes
16
answers
198k
views
C++ sorting and keeping track of indexes
Using C++, and hopefully the standard library, I want to sort a sequence of samples in ascending order, but I also want to remember the original indexes of the new samples.
For example, I have a set, ...
282
votes
26
answers
320k
views
List columns with indexes in PostgreSQL
I would like to get the columns that an index is on in PostgreSQL.
In MySQL you can use SHOW INDEXES FOR table and look at the Column_name column.
mysql> show indexes from foos;
+-------+------...
278
votes
4
answers
292k
views
Can I add a UNIQUE constraint to a PostgreSQL table, after it's already created?
I have the following table:
tickername | tickerbbname | tickertype
------------+---------------+------------
USDZAR | USDZAR Curncy | C
EURCZK | EURCZK Curncy | C
EURPLN | EURPLN ...
277
votes
9
answers
99k
views
Is the primary key automatically indexed in MySQL?
Do you need to explicitly create an index, or is it implicit when defining the primary key? Is the answer the same for MyISAM and InnoDB?
271
votes
11
answers
164k
views
Postgres unique constraint vs index
As I can understand documentation the following definitions are equivalent:
create table foo (
id serial primary key,
code integer,
label text,
constraint foo_uq unique (code, label));...
262
votes
6
answers
153k
views
How important is the order of columns in indexes?
I've heard that you should put columns that will be the most selective at the beginning of the index declaration. Example:
CREATE NONCLUSTERED INDEX MyINDX on Table1
(
MostSelective,
...
260
votes
5
answers
224k
views
How to list indexes created for table in postgres
Could you tell me how to check what indexes are created for some table in postgresql ?
256
votes
4
answers
129k
views
Why does PostgreSQL perform sequential scan on indexed column?
Very simple example - one table, one index, one query:
CREATE TABLE book
(
id bigserial NOT NULL,
"year" integer,
-- other columns...
);
CREATE INDEX book_year_idx ON book (year)
EXPLAIN
...
251
votes
5
answers
678k
views
pandas: multiple conditions while indexing data frame - unexpected behavior
I am filtering rows in a dataframe by values in two columns.
For some reason the OR operator behaves like I would expect AND operator to behave and vice versa.
My test code:
df = pd.DataFrame({'a': ...
250
votes
6
answers
123k
views
What's the difference between using INDEX vs KEY in MySQL?
I know how to use INDEX as in the following code. And I know how to use foreign key and primary key.
CREATE TABLE tasks (
task_id int unsigned NOT NULL AUTO_INCREMENT,
parent_id ...