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
0
votes
1 answer

pandas loc search by part of a string not using regexp

import pandas as pd data = {'name': ['HelloWorld', 'ByeWorld'], 'physics': [22, 33], 'chemistry': [44, 55]} a = pd.DataFrame(data) b = a.loc[a['name'] == 'Hello'] print(b) This code would not return any rows, but I would like…
filtertips
  • 801
  • 3
  • 12
  • 25
0
votes
0 answers

pandas loc code to replace column contents no longer working following software update, key error but key is there

I have some code which looks for the full name of a browser in a column and where found overwrites with a short name. this is to allow simplified filtering without version numbers in visualisations. This code has worked fine for most of 2020. I get…
MizzH
  • 13
  • 2
0
votes
2 answers

Adjust numeric scale for a set of rows in a more effective/Pythonic way?

So my dataset is survey data, where each row shows a question and a unique respondent's numeric response to that question. Unfortunately, the scale was backwards for some question (aka 1s should be 4s and 4s should be 1s). I came up with the most…
0
votes
0 answers

Some problems in python Pandas and KeyError

Traceback (most recent call last): File "C:\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 70, in…
Ben WMean
  • 1
  • 1
0
votes
2 answers

Access a row from a df based on a column value

I am trying to find out the rows where the reliability is <0.70, but the output seems to include rows where Reliability is 0.70 as well. What could be wrong? Original DF: po_id po_name product year measure rate denominator numerator is_reported…
anu
  • 17
  • 7
0
votes
1 answer

How to change categorical variables in a for loop using a list with variable to change

I would like to simplify the number of categories for one variable. The piece of code below is working: df.loc[(df['category'] == 'cat1')|(df['category'] == 'cat2')|(df['category'] == 'cat3')|...|(df['category'] == 'catn'),'category'] ==…
Pierre
  • 13
  • 4
0
votes
1 answer

Slice mutilple columns that are not next to each other in dataframe

I want to slice metope columns that are located several columns away from each other. I'm trying to write code that easy without having to write the code repeatedly: df (See below for example) where columns are from A to H, with many rows…
moman
  • 1
  • 3
0
votes
1 answer

Find date in column, append adjacent column value to new column - all in one df?

I have a timeseries df with 5 years of stock index prices (so df has 2 columns Date and Price. I then have a new column '3M forward date' which is just the original Date column + 3 months. I'm then trying to create a '3M forward Price' column. This…
breaker7
  • 113
  • 8
0
votes
2 answers

Pandas creating and populating a new dataframe efficiently (?)

I'm creating a new DataFrame from scratch, but I'm not sure the way I'm doing it is the most efficient way. I'm creating: column Never where 3070 = 1 column Occasional 1100 = 1 column Frequent 2200 = 1 I'm also creating a new column Police: where…
laminado
  • 69
  • 1
  • 3
  • 11
0
votes
1 answer

Filtering and replacing the value in DataFrame

I am trying to filter the rows of a DataFrame according to the below conditions and want to replace the values by a new value of a column. The error I am getting is: TypeError: unsupported operand type(s) for &: 'float' and 'bool' The picture of df…
0
votes
1 answer

The loc function in jupyter does not work for filtering my dataframe

I am pretty new to Jupyter and have imported a data set. That worked fine. Then I wanted to use the loc function to get just one specific value in a specific column. However the loc function simply doesn't work on my Jupyter notebook. I have…
Jose
  • 3
  • 3
0
votes
1 answer

.loc to filter database by date

I have learning python over the past few weeks and I have an issue with the .loc function. I have a dataframe (BAC) comprised of daily equity prices and dates as an index (which seems to be a datetime object). I want to filter only the dates in 2008…
user2952666
  • 55
  • 1
  • 4
0
votes
1 answer

Pandas - How to extract columns from a dataframe using another dataframe?

I am looking to extract columns from a dataframe using another dataframe. I don't want to hard code the column headers into the code as the data comes from a csv and columns can be added with different headers. Tried with .loc and using iterations,…
Broton
  • 7
  • 2
0
votes
1 answer

How to search a pandas DataFrame for the first row satisfying set of conditions without reading the rest of the rows?

I have a huge DataFrame (~4 million rows) and I need to search it for a row which has specific columns values for about a million time. Based on the conditions governing my problem, there is only one true answer (one row) for each query. So as soon…
MHDMYZ
  • 1
  • 2
0
votes
1 answer

Dataframe column locating min and max value depeding of an ID

I'm wondering how to optimize a part of code to remove a loop which takes forever since I have around 350 000 IDs. Here is the current code, which is not optimal and takes quite a while. I'm trying to get it working better and if possible removing a…