Questions tagged [pandas-loc]

For questions about the [pandas] loc indexer. This tag should only be used for questions primarily about pandas loc or the behavior of this indexer. Be sure to also include the [pandas] tag on your question.

The pandas loc indexer allows for label based data selection from pandas DataFrames on multiple axes simultaneously.

It is primarily used in data Selection by label and Setting data at specific locations in a pandas DataFrame.

There are loc indexers for both DataFrames and Series.

381 questions
918
votes
7 answers

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 different. To me, they seem interchangeable in large…
AZhao
  • 13,617
  • 7
  • 31
  • 54
122
votes
3 answers

Python: Pandas Series - Why use loc?

Why do we use 'loc' for pandas dataframes? it seems the following code with or without using loc both compile anr run at a simulular speed %timeit df_user1 = df.loc[df.user_id=='5561'] 100 loops, best of 3: 11.9 ms per loop or %timeit…
Runner Bean
  • 4,895
  • 12
  • 40
  • 60
63
votes
1 answer

python pandas loc - filter for list of values

This should be incredibly easy, but I can't get it to work. I want to filter my dataset on two or more values. #this works, when I filter for one value df.loc[df['channel'] == 'sale'] #if I have to filter, two separate columns, I can do…
jeangelj
  • 4,338
  • 16
  • 54
  • 98
25
votes
2 answers

Why/How does Pandas use square brackets with .loc and .iloc?

So .loc and .iloc are not your typical functions. They somehow use [ and ] to surround the arguments so that it is comparable to normal array indexing. However, I have never seen this in another library (that I can think of, maybe numpy as something…
Conner Phillips
  • 403
  • 4
  • 7
18
votes
2 answers

Use of loc to update a dataframe python pandas

I have a pandas dataframe (df) with the column structure : month a b c d this dataframe has data for say Jan, Feb, Mar, Apr. A,B,C,D are numeric columns. For the month of Feb , I want to recalculate column A and update it in the dataframe i.e. for…
Data Enthusiast
  • 521
  • 4
  • 12
  • 22
14
votes
3 answers

Pandas dataframe creating multiple rows at once via .loc

I can create a new row in a dataframe using .loc(): >>> df = pd.DataFrame({'a':[10, 20], 'b':[100,200]}, index='1 2'.split()) >>> df a b 1 10 100 2 20 200 >>> df.loc[3, 'a'] = 30 >>> df a b 1 10.0 100.0 2 20.0 200.0 3 30.0…
Zhang18
  • 4,800
  • 10
  • 50
  • 67
12
votes
4 answers

Select row using the length of list in pandas cell

I have a table df a b c 1 x y [x] 2 x z [c,d] 3 x t [e,f,g] Just wondering how to select the row using the length of c column such as df.loc[len(df.c) >1] I know this is not right.... what should be the right one?
Kevin
  • 587
  • 1
  • 7
  • 17
11
votes
2 answers

Pandas DataFrame Assignment Bug using Dictionaries of Strings and Floats?

Problem Pandas seems to support using df.loc to assign a dictionary to a row entry, like the following: df = pd.DataFrame(columns = ['a','b','c']) entry = {'a':'test', 'b':1, 'c':float(2)} df.loc[0] = entry As expected, Pandas inserts the…
ThatNewGuy
  • 197
  • 11
8
votes
1 answer

Overwriting Nan values with .loc in Pandas

I tried to solve the required task with the following code line: df['Age'][np.isnan(df["Age"])] = rand1 But this raises a "SettingWithCopyWarning" and I think locating the Nan values in the dataframe (Column 'Age') by using the .loc feature might…
ErnieandBert
  • 91
  • 1
  • 1
  • 8
6
votes
2 answers

Update Pandas df Given String Query

As a result of data introduced by users in the interface I have a string query-like. query = '(ColA=="7") & (ColB=="3") & (ColC=="alpha") & (ColD=="yu")' Now I want to update a column of the df based on those conditions, assigning it a variable Z.…
burn1000
  • 91
  • 6
6
votes
1 answer

setting values in a pandas dataframe using loc - multiple selection criteria allowing setting value in a different column

I have a database with multiple columns and rows. I want to locate within the database rows that meet certain criteria of a subset of the columns AND if it meets that criteria change the value of a different column in that same row. I am…
wiseass
  • 543
  • 2
  • 5
  • 11
5
votes
2 answers

Change value in pandas after chained loc and iloc

I have the following problem: in a df, I want to select specific rows and a specific column and in this selection take the first n elements and assign a new value to them. Naively, I thought that the following code should do the job: import seaborn…
My Work
  • 2,143
  • 2
  • 19
  • 47
5
votes
2 answers

Pandas loc error: 'Series' objects are mutable, thus they cannot be hashed

I need some help with a problem in handling pandas DataFrames. Here is the Code: df.drop(df.index[0], inplace=True) df.columns = ['Mic. No.', 'X', 'Y', 'Z', 'Re. Pre.', 'Im. Pre.'] df['Pre'] = df['Re. Pre.'] + df['Im. Pre.'] * 1j df.drop(['Mic.…
Samuel K.
  • 180
  • 1
  • 1
  • 11
5
votes
2 answers

Inserting a tuple into an empty pandas dataframe

I would like to insert a row into an empty DataFrame. However, this seems to fail for a DataFrame with predefined indices and when the elements include a tuple or list prompting the error: ValueError: setting an array element with a sequence. The…
Christian
  • 991
  • 2
  • 13
  • 25
5
votes
1 answer

how to force pandas .loc to return series

I have a Pandas DataFrame with NAME index and one of the columns called CLASS: df = CLASS NAME John Math John Science Lisa Music Now if I extract df.loc['John', 'CLASS'], it returns a Series, but if I extract df.loc['Lisa',…
Zhang18
  • 4,800
  • 10
  • 50
  • 67
1
2 3
25 26