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

Pandas select rows where relative column values exist in DataFrame

Let's say you have a dataframe like this: >>> df = pd.DataFrame({ 'epoch_minute': [i for i in reversed(range(25090627,25635267))], 'count': [random.randint(11, 35) for _ in range(25090627,25635267)]}) >>> df.head() epoch_minute …
aweeeezy
  • 806
  • 1
  • 9
  • 22
2
votes
2 answers

Python - Double condition data.loc

I have the following data frame: Date Value1 Value2 01-01-01 01 01 02-01-01 02 00 03-01-01 03 01 04-01-01 04 101 On this data frame I would like to select just the rows having Value2==0 and…
steve
  • 511
  • 1
  • 9
  • 33
2
votes
1 answer

Try using .loc[row_indexer,col_indexer] = value instead

Using python, there are something wrong. resource1 #"dataframe" and, resource1.loc[(resource1["code"] == ""), "code"] = "nocode" then, error message is here. ~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:537: …
JINWOO LEE
  • 21
  • 1
  • 2
2
votes
1 answer

Pandas Matching nearest Datetime values in 2 columns - type integer/long error

I have a DataFrame, D1: Date Symbol ICO_to 5/28/2017 18:00 MYST 5/30/2017 5/29/2017 18:00 MYST 5/30/2017 5/30/2017 18:00 MYST 5/30/2017 6/1/2017 18:00 MYST 5/30/2017 6/2/2017 18:00 MYST 5/30/2017 6/3/2017 18:00 MYST…
Cole Starbuck
  • 603
  • 3
  • 11
  • 21
2
votes
2 answers

Pandas Apply and Loc - efficiency and indexing

I want to find the first value after each row that meets a certain criteria. So for example I want to find the first rate/value (not necessarily the first row after) after the current row that increased 5%. The added column would be the last…
Liam Hanninen
  • 1,525
  • 2
  • 19
  • 37
2
votes
1 answer

Pandas df.loc comparing-floats-condition never works

df[['gc_lat', 'gc_lng']] = df[['gc_lat', 'gc_lng']].apply(pd.to_numeric, errors='ignore') df_realty[['lat', 'lng']] = df_realty[['lat', 'lng']].apply(pd.to_numeric, errors='ignore') for index, row in df.iterrows(): gc_lat =…
vazhega
  • 31
  • 6
2
votes
2 answers

Select rows in dataFrame with the same index using python

I want to ask you, How to select rows that have the same index number in a DataFrame. Example: df= A, B, C, 0 1. 2. 1. 1 2. 2. 2. 2 2. 2. 2. 3 3. 3. 4. A, B, C, 0 1. 2. 1. 1 2. 2. 2. 2 2. 2. 0. 3 3. 3. 4. A, B, C, 0 1. 2. 1. 1 2. 2. 2. 2…
Jonathan Pacheco
  • 531
  • 1
  • 6
  • 16
2
votes
1 answer

Updating Panel slice

I need to update a panel slice with some values retreated from a dataframe. Even if I don't get back any error it doesn't work. What it's wrong ? df = pd.DataFrame(np.random.rand(10, 4), columns=['sd', 'ed', 'sbc',…
Cursore
  • 33
  • 1
  • 5
2
votes
1 answer

Returning subset of each group from a pandas groupby object

I have the multilevel dataframe that looks like: date_time name note value list index 1 0 2015-05-22 05:37:59 Tom 129 False 1 2015-05-22 05:38:59 Tom …
user5421875
1
vote
2 answers

Apply definition on multiple columns based on conditions- python

i have dataframe that i want when one column equal to '02' apply the definition to 5 others columns. this is my code: mask = (df['A']=='02') z_valid = df[mask] df.loc[((mask) & (df['B'] !=0)), 'B'] = z_valid['B'].apply(def) df.loc[((mask) & (df['C']…
so.n
  • 35
  • 5
1
vote
1 answer

How filter an inner dataframe based on the row of an outer dataframe

**My goals: ** filter an inner dataframe based on the row (then the values) of an outer dataframe Let's consider three dataframes: first a list of shop with detail about the shop: Shop = pd.DataFrame({ 'Shop_number':…
send_
  • 11
  • 2
1
vote
2 answers

KeyError when filter pandas dataframe by column with particular key:value pair

My df looks like the following col1 col_x ... {"key_x":"value1"} ... None ... {"key_x":"value2"} How to select all items with {"key_x":"value1"} in col_x ? col_x values could be dict or None. What I've…
Paul Serikov
  • 2,550
  • 2
  • 21
  • 36
1
vote
1 answer

Why does converting a column from datetime to string in pandas not work using .loc?

I'm using pandas to load data from Excel with the resulting DataFrame containing both strings and dates. The columns containing strings are of dtype "object" while the date-columns are of dtype "datetime64[ns]". At some point in my code I need to…
Archernar
  • 33
  • 6
1
vote
1 answer

What's the difference between these 2 methods of Series filtering? (with or without lambda)

I have a data Series called Snow (the amount of snow in different months of the year). These two lines of code produce the same results (at least seems so!) So I just wanted to know the difference. import pandas as pd snow.loc[(snow.index.month==1)…
1
vote
1 answer

How do I change a pandas row value, for a certain column, for a certrain date (datetimeindex) in a dataframe?

I have a pd like this: DATE delivery 2020-01-01 1 2020-01-01 11 2020-01-01 10 2020-01-01 9 2020-01-01 8 .. 2023-03-02 5 2023-03-02 4 2023-03-02 3 2023-03-02 2 2023-03-02 11 Index is…
cJc
  • 813
  • 1
  • 11
  • 33