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

Data frame filtered with loc removing index

Consider the data frame df = pd.DataFrame(numpy.random.randint(0,10,size=(5, 4)), columns=list('ABCD')) df A B C D 0 5 8 0 4 1 7 4 9 0 2 8 1 1 8 3 2 7 6 6 4 4 3 3 0 I would like to filter with loc (the result will be…
Kenny
  • 1,902
  • 6
  • 32
  • 61
5
votes
1 answer

Apply pandas.to_numeric to selected subset of columns using loc in pandas DataFrame

How to apply pandas.to_numeric to a subset of DataFrame selected using .loc[]? E.g. consider this DataFrame: df = pd.DataFrame(index=pd.Index([1, 2, 3])) df['X'] = ['a', 'a', 'b'] df['Y'] = [1, 2, 3] df['Z'] = [4, 5, 6] df['Y'] =…
4
votes
1 answer

Pandas dataframe select row by index and column by name

Is there any way to select the row by index (i.e. integer) and column by column name in a pandas data frame? I tried using loc but it returns an error, and I understand iloc only works with indexes. Here is the first rows of the data frame df. I am…
Ricardo Milhomem
  • 159
  • 2
  • 3
  • 11
4
votes
1 answer

Sum Rows at Bottom of Pandas Dataframe

trying to sum rows for specific columns in pandas. have: df = name age gender sales commissions joe 25 m 100 10 jane 55 f 40 4 want: df = name age gender sales commissions joe 25 m 100 …
nia4life
  • 333
  • 1
  • 5
  • 17
4
votes
1 answer

Apply loc for 2 columns values Pandas

I´m tying to loc a dataframe with 2 columns parameters: if I do paises_cpm = df.loc[a]is working but if I do paises_cpm = df.loc[a,b] I receive an error: IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and…
Martin Bouhier
  • 361
  • 2
  • 6
  • 19
4
votes
1 answer

Most efficient way of joining dataframes in pandas: loc or join?

Suppose I have two dataframes; one holds transactions, trans and the other holds product information, prod, and I want to join the product prices, the variable price, on to the transaction data frame, repeating them down for each column. Which of…
Superpronker
  • 309
  • 4
  • 10
3
votes
2 answers

Compare data series columns based on a column that contains duplicates

I have a dataset that I've created from merging 2 df's together on the "NAME" column and now I have a larger dataset. To finish the DF, I want to perform some logic to it to clean it up. Requirements: I want to select the unique 'NAME' but I want to…
aero8991
  • 239
  • 1
  • 13
3
votes
3 answers

Applying an IF condition in multiple columns with pandas

I have an ascii file as following (a sample) id lon lat val1 val2 val3 1 22 38 67 66 87 89 2 23.5 39 56 10 90 98 3 22.5 38.5 34 45 56 78 For specific points (lat,lon) I want to set to zero the variables val1, val2,val3. e.g. for lon=22, lat=38…
Nat
  • 325
  • 2
  • 13
3
votes
3 answers

Pandas date index loc between dates throws KeyError when edge date is not in dataframe

I can't understand why I'm getting KeyError: Timestamp('...') when using loc on date index. With given df: dtypes are datetime64[ns], int, int, DATE1 is index DATE1 VALUE2 VALUE3 2021-08-20 00:00:00 11 424 2021-08-21…
Kalik
  • 175
  • 1
  • 11
3
votes
2 answers

Passing a list to pandas loc method

I'd like to change the values of certain columns in a pandas dataframe. But I can't seem to do if I pass a list of columns inside loc. df = pd.DataFrame({ "ID" : [1, 2, 3, 4, 5], "QA_needed" : [0, 1, 1, 0, 1], "QC_needed" : [1, 0, 1, 0,…
Diego
  • 392
  • 3
  • 16
3
votes
1 answer

How do you use loc in pandas with more than one condition?

I'm looking at this example from the documentation: df.loc[lambda df: df['shield']==8] How do I extend this with an OR condition? I'm trying to do this but it doesn't work: df.loc[lambda df: df['shield']==8 or df['max_speed'] ==1] Also, as a side…
Noobcoder
  • 326
  • 3
  • 14
3
votes
1 answer

Is it possible to use a loc inside a loc in Pandas to replace a value?

Let me first sketch the problem I am trying to solve. I am trying to replace a value '-1' with another value in the same column, based on two other values in the row containing the '-1'. To be more clear, here is an example. In the dataframe…
3
votes
1 answer

Finding the row number for the header row in a CSV file / Pandas Dataframe

I am trying to get an index or row number for the row that holds the headers in my CSV file. The issue is, the header row can move up and down depending on the output of the report from our system (I have no control to change this) code: ht =…
Runawaygeek
  • 115
  • 3
  • 13
3
votes
3 answers

interacting over a dateframe with functions

if I have a date frame like this: N EG_00_04 NEG_04_08 NEG_08_12 NEG_12_16 NEG_16_20 NEG_20_24 \ datum_von 2017-10-12 21.69 15.36 0.87 1.42 …
may
  • 1,073
  • 4
  • 14
  • 31
3
votes
1 answer

Pandas loc() method with boolean array on axis 1

I am experimenting with the Pandas loc() method, used with boolean arrays as arguments. I created a small dataframe to play with: col1 col2 col3 col4 0 a 1 2 3 1 b NaN NaN 6 2 c NaN 8 …
im7
  • 653
  • 2
  • 10
  • 28
1
2
3
25 26