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

Python/ Pandas: Finding a left and right max

I've got a pandas dataframe with an area in the first column and 8 years of quarterly data in the rest. There's about 4400 rows. Here is a sample: idx Q12000 Q22000 Q32000 Q42000 Q12001 Q22001 Q32001 Q42001 …
CJ H
  • 59
  • 9
2
votes
2 answers

How do I delete a row of indexes with the starting and ending index in a dataframe(python)?

For example: INDEX FRUIT 0: "banana" 1: "apple" 2: "donut" 3: "pizza" 4: "noodles" 5: "ice-cream" 6: "grapefruit" Usually, in order to drop rows 2-5 inclusive, I use drop.(df.index[[2,3,4,5]])…
2
votes
2 answers

Pandas How to filter on Null values and zero values in the same column

I have been trying to fix this syntax but with no luck. I would be grateful if someone could help me. 1 vat.loc[(vat['Sum of VAT'].isnull()) & 2 (vat['Sum of VAT'] == 0) & 3 (vat['Comment'] == 'Transactions 0DKK') & 4 …
Darko86
  • 65
  • 1
  • 4
2
votes
1 answer

python dataframe concatenate based on a chosen date

Say I have the following variables and dataframe: a = '2020-04-23 14:00:00+00:00','2020-04-23 13:00:00+00:00','2020-04-23 12:00:00+00:00','2020-04-23 11:00:00+00:00','2020-04-23 10:00:00+00:00','2020-04-23 09:00:00+00:00','2020-04-23…
ds882
  • 105
  • 11
2
votes
1 answer

df.loc produces an error if the dtype of the index is mixed int/str

I have a data set with mixed index values, int and str, which df.to_csv reads as an object. If I try to slice the rows this does not work, I get a TypeError. I know I can work around it by changing the index dtype, but I would like to understand why…
Martin S.
  • 21
  • 2
2
votes
1 answer

Python loc + isin returns FutureWarning (elementwise comparison failed)

df = df.loc[df['var'].isin(df2['this'].unique().tolist())] The code above gives me a FutureWarning and I can't work out a way around it. Is this a bug or…
Stphn
  • 107
  • 8
2
votes
3 answers

How to pass a "Take All" parameter in pandas loc filter condition?

I have a function with a parameter (in this case: "department") to filter (df.loc[(df['A'] == department) specific data out of my dataset. In one case, I want to use this specific function but instead of filtering the data, I want to get all the…
ProjektWeinheim
  • 201
  • 3
  • 10
2
votes
3 answers

Pandas loc multiple conditions

I have a dataframe and I want to delete all rows where column A is equal to blue and also col B is equal to green. I though the below should work, but its not the case. Can anyone see the problem df=df.loc[~(df['A']=='blue' & df['B']=='green')]
fred.schwartz
  • 2,023
  • 4
  • 26
  • 53
2
votes
1 answer

How to find the row value of other column(column2), if my row value isin same column(column1) in other row

I have DataFrame like this: month CPT 5/1/2017 aa 5/1/2017 bb 5/1/2017 cc 6/1/2017 aa 6/1/2017 cc 6/1/2017 dd 7/1/2017 aa 7/1/2017 bb 7/1/2017 cc I want new column(old_bill) where in I need column(month) values…
2
votes
1 answer

how to select NaN rows and assign value_A to a new column? code df.loc['condition_1' & 'condition_2' & 'df.column_a == ''] = 'value_a'

I have the data similar as below: A B C 0 M M M 1 Y M M 2 Y NaN NaN 3 Y Y etc what i need is: A B C F 0 M M M 3 1 Y M M 4 2 Y NaN NaN 0 3 Y Y etc 5 I don't know how to deal with…
Sean.H
  • 640
  • 1
  • 6
  • 18
2
votes
1 answer

How to get df.loc to just return the value (number) from a specific cell of a dataframe?

I'm working with data on the frequency that college basketball teams take 2's and 3's. I plan on multiplying the frequency at which they take 3's by 3, and adding it to the frquency at which they take 2's by 2. A function will be doing this. That…
Cornel Westside
  • 117
  • 1
  • 11
2
votes
2 answers

Condition between duplicated values in a column

Every customer is duplicated when they have more than one plan. I want to set the status to the customer: If they have every product with 'canceled_at' filled, the customer status is cancelled, but when it's not every product with the canceled_at…
2
votes
2 answers

Pandas Apply with condition

I have customers duplicates with different status because there is a row for each customer subscription/product. I want to generate a new_status for the customer and for it to be 'canceled', every subscription status must be 'canceled' together. I…
2
votes
2 answers

Group by with sum conditions

I have the following df and I'd like to group it by Date & Ref but with sum conditions. In this respect I'd need to group by Date & Ref and sum 'Q' column only if P is >= than PP. df = DataFrame({'Date' : ['1', '1', '1', '1'], 'Ref'…
Rose
  • 203
  • 2
  • 10
2
votes
1 answer

Pandas - flatten the diagonal values of dataframe returned from an apply loc operation

I have two dataframes -- for each row in df I want to look up the matching epoch_minute - lag and grab the corresponding average_hc value. >>> df.head() epoch_minute headcount 0 25640940 8 1 25640939 7 2 25640938…
aweeeezy
  • 806
  • 1
  • 9
  • 22