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

How to search multiple things with .loc

If there is a more effecient way please let me know. pk.loc[pk['Type 1'] == 'Grass'] I want to do this but also search with Type 2 being Ground, but I can't find the syntax.
Aids_man
  • 7
  • 1
-1
votes
1 answer

Get column dynamically in loc based on condition

I have a dataframe : df.head() : col_a col_a Month 1 Month 2 Month 3 Month 4 Month 5 Month 6 10 2 20 6 44 3 55 1 86 4 67 5 What I want do : I want values of col_a to be assigned based on col_b in a specific month, for…
Yash Pathak
  • 7
  • 1
  • 5
-1
votes
1 answer

how do I perform maths on pandas series

I have defined a function as shown. def calculate_mass_loss(group, idnum): """ Calculates mass loss on star referenced by idnum due to high mass stars Parameters ---------- group: `pd.DataFrame` Dataframe of all stars …
-1
votes
1 answer

I have a table with a time index. I want to choose from it only some of the days that are not continuous I did so and did not work

that is my date I want to choose myDates=['2021-02-24', '2021-02-26','2021-02-27', '2021-03-06', '2021-04-4', '2021-04-05', '2021-04-06', '2021-04-07', '2021-04-08','2021-04-13', '2021-04-14', '2021-04-15', '2021-04-16','2021-04-17', …
-1
votes
2 answers

Python DF issue. Match on the basis of column value

I have a DF which is quite big. A snippet like the one shown below. SrNo | Merchant | Revenue | Currency 1 | UBER SR | 123 | INR 2 | UBER (SR)| 123 | INR 3 | SR UBER | 123 | INR 4 | ZOMATO SR| 123 | INR 5 |…
-1
votes
1 answer

Python: Return First value based on dates for each product

I am looking for an iterative way of creating a subset of my pandas dataframe based on the date and product. I would like to keep the first row for each product within a 2 week window. So for df…
Ksh
  • 85
  • 4
-1
votes
1 answer

pandas loc attribute behaves as not expected

Could someone please explain why loc behaves different from what I expect? The code is educated_less = df.loc[ ~df['education'].isin(['Masters', 'Bachelors', 'Doctorate'])] It seems that loc should return only one column 'education' following the…
Nikita
  • 3
  • 2
-1
votes
1 answer

Error when .loc() rows with a list of dates in pandas

I have the following code: import pandas as pd from pandas_datareader import data as web df = web.DataReader('^GSPC', 'yahoo') df['pct'] = df['Close'].pct_change() dates_list = df.index[df['pct'].gt(0.002)] df2 = web.DataReader('^GDAXI',…
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
-1
votes
1 answer

Why am I getting a Key Error with a loc statement on a dataframe with a known column that prints out correctly

I get a key error on a column that exists under the name that I attempt to use in a loc statement. product = products_df.loc[products_df['product_id'] == 6] Before the loc statement, I printed out the first five rows of the data…
-1
votes
2 answers

Combining multiple rows in a pandas dataframe

I want to select both the rows 489-493 and the rows 503-504 in this dataframe. I can slice them separately by df.iloc[489:493] and df.iloc[503:504], respectively, but am not sure how to combine them? I have tried using df[(df.State…
Bluetail
  • 1,093
  • 2
  • 13
  • 27
-1
votes
1 answer

Need to locate a specific value from a dataframe based on a loc search

I'm working with this dataframe. I need to find a specific index number and, based on the inspect_date, I need to find the most recent name. I tried using the following code, but it isn't working: last_name=…
-1
votes
3 answers

Selecting rows where column is not null and equal to a (string) value

So basically I want select all rows where Col A is equal to the string 'hey'. My problem is that Col A can contain null/nan's therefore I get a TypeError: invalid type comparison. When executing: df.loc[df['A'] == 'hey'] I then made another…
Thor Bjorn
  • 1
  • 1
  • 1
-2
votes
1 answer

using an .loc and If statment

enter image description here Use the .loc method, select the column rating for the rows of df where company_location equals "U.K." Store it in a variable called uk_ratings. uk_ratings = choco_df.loc[:, "rating"] if company_location == "U.K."…
Sarah
  • 3
  • 3
-2
votes
1 answer

returning a view versus a copy warning

I am trying to create a new column that checks if there's a string in my values using the following method: user['validacao_LP']=user['MEDIA_SOURCE'].str.contains('LP') But I keep getting the following message: :1:…
-2
votes
1 answer

python iloc errors new column data frame

I have this python script, this is just a part of it, it works but for just 2 lines I'm having troubles: not_marketo.loc['Marketo DEP'] = "NO" yes_marketo.loc[:,'Marketo DEP'] = C I have tried all the possible ways: not_marketo['Marketo DEP'] =…
Berny
  • 113
  • 11
1 2 3
25
26