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
0 answers

Displaying rows where condition matches some part of string in pandas

We use fund_data.loc[fund_data['SubVertical']=="Data Analytics platform"] to display all details of the columns containing these('Data Analytics platform') strings in the condition of a column in pandas, but suppose we want to display rows…
noswear
  • 311
  • 1
  • 6
  • 27
0
votes
1 answer

How to access a number in a dataframe

All, I have a df that looks like this lib1m markdate tag purpose currencyBench 0 -0.08900 2018-03-01 LIBOR REUTERS JPY 1 0.49663 2018-03-01 LIBOR REUTERS GBP 2 -0.59900 2018-03-01 STIBOR REUTERS …
SBad
  • 1,245
  • 5
  • 23
  • 36
0
votes
0 answers

Pandas datetime matching nearest date - indexing error 'dtype='int64')' is an invalid key'

I have a DataFrame, D1 that looks as follows: close Date Symbol ICO_to 2.71 6/12/2017 18:00 MYST 5/30/2017 2.18 6/13/2017 18:00 MYST 5/30/2017 2.1 6/14/2017 18:00 MYST 5/30/2017 2.17 6/15/2017 18:00 MYST 5/30/2017 2.34 6/16/2017 18:00 MYST…
Cole Starbuck
  • 603
  • 3
  • 11
  • 21
0
votes
1 answer

Creating new row values within same pandas dataframe based on difference of other row values

Below is an existing df data = np.array([['','Market','Product Code','Week','Sales','Units'], ['Total Customers',123,1,500,400], ['Total Customers',123,2,400,320], ['Major Customer 1',123,1,100,220], …
jwlon81
  • 339
  • 3
  • 15
0
votes
1 answer

How to create a new column in python under multiple conditions?

I would like to create a new column under the following condition: So basically I have two column Majoy car and Major housetype. I would let all the 'nocar' within Majoy car AND 'Rented' within Major housetype to merge to a new column. My DataFrame…
Guo Yijin
  • 25
  • 6
0
votes
1 answer

Pandas loc does not work to subset DataFrame when using a variable

I am fairly new to Python, especially pandas. I have a DataFrame called KeyRow which is from a bigger df: KeyRow=df.loc[df['Order'] == UniqueOrderName[i]] Then I make a nested loop for i in range (0,len(PersonNum)): …
Questions
  • 117
  • 3
  • 3
  • 9
0
votes
0 answers

How turn iterrows if statements into vectorized function or other faster method in Pandas

I have been searching for a way to replace itterrows with vectorization and coming up blank. I have this code which i believe to be working correctly using itterows which is taking forever. sm_state = 0 fst_state = 0 time = 0 for index, row in…
3ricVos
  • 1
  • 2
0
votes
1 answer

How to change specific cell values in a pandas dataframe column series based on multiple conditions?

I am trying to replace all values in a pandas dataframe column df.column_A if they fall within the range of 1 to 10. However, when I do: df.loc[(1 < df.column_A < 10), "Column_A"] = 1 Produces: ValueError: The truth value of a Series is ambiguous.…
sudonym
  • 3,788
  • 4
  • 36
  • 61
0
votes
0 answers

Changing column values Pandas Python LOC ILOC SETVALUE Difficulty

I encounter a strange problem in pretty big piece of code. Normally, I use .loc to change particular items in a certain column within a loop while using a row_index variable as help. Lets assume to have the following: df['Column1'] = 0 list =…
0
votes
0 answers

Dataframe loc - unexpected behaviour

I have a dataframe df which looks like this: Order Type Quantity 2015-04-30 Buy 200 2015-05-06 Buy 168 2015-05-08 Sell 368 2015-06-04 Buy 126 2015-06-10 Buy 129 2015-06-17 …
ralphW
  • 1
  • 2
0
votes
1 answer

pandas.DataFrame.xs() returns error on multi index when `level` arg used

I have a potential pandas bug, or maybe I've just been staring at this too long. I have not had issues using xs on a multi index before. Code is bellow and I've verified that the error occurs on both Python 2 on pandas version 0.16.2 and Python 3…
htln
  • 1
  • 1
0
votes
1 answer

Cryptic warning pops up when doing pandas assignment with loc and iloc

There is a statement in my code that goes: df.loc[i] = [df.iloc[0][0], i, np.nan] where i is an iteration variable that I used in the for loop that this statement is residing in,np is my imported numpy module, and df is a DataFrame that looks…
AKKO
  • 973
  • 2
  • 10
  • 18
-1
votes
0 answers

Upgraded Python pandas to 1.4.4 and now I'm getting this error when running multiple column .loc statement

I have code in production that uses .loc to update multiple columns in one dataframe. Prior to the upgrade to Pandas 1.4.4, this code was working and now it's throwing an error. Any ideas on how to fix this? df = pd.DataFrame([['yes','no','maybe']],…
Keri
  • 17
  • 2
-1
votes
1 answer

Trying to find rows which end in .99 cents // Python // Multiple Conditionals // .loc // Manipulation inside .loc

Trying to find rows of a column which end in .99 cents len(df_official.loc[(df_official.price_duration_days >= 7) & (df_official.shop.isin(official_stores)) & (df_official['Days From Release'] <= 3) & (str(df_official.y)[-3:] == '.99')])…
-1
votes
1 answer

Problem with selecting individual rows from pandas data frame

I would like to select rows using condition on columns like "sex" = "male". I normally used loc function on DataFrame. import pandas as pd dane = pd.read_csv('insurance.csv') dane.info() the result is: