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

How to add a string to every even row in a pandas dataframe column series?

I am new to pandas. I want to add a new column to a pandas dataframe df and assign "Start" to every odd row and "Stop" to every even row. However, when I do df.iloc[1::2, :] = "Start", I am inserting a new row at every 2nd position with the "Start"…
sudonym
  • 3,788
  • 4
  • 36
  • 61
3
votes
2 answers

Subtract one row from another in Pandas DataFrame

I am trying to subtract one row from another in a Pandas DataFrame. I have multiple descriptor columns preceding one numerical column, forcing me to set the index of the DataFrame on the two descriptor columns. When I do this I get a KeyError on…
KidMcC
  • 486
  • 2
  • 7
  • 17
3
votes
2 answers

KeyError when using s.loc and s.first_valid_index()

I have data similar to this post: pandas: Filling missing values within a group That is, I have data in a number of observation sessions, and there is a focal individual for each session. That focal individual is only noted once, but I want to fill…
M.A.Kline
  • 1,697
  • 2
  • 19
  • 29
2
votes
1 answer

Mapping value based on two dataframe (error: an only compare identically-labeled Series objects)

Table t1 id mins maxs 0 43852 11 11 1 63087 14 15 2 63106 14 15 3 63155 14 15 Table t2 idx cons 0 1 1.00 1 2 0.95 2 3 0.90 3 4 0.85 4 5 0.80 5 6 0.70 6 7 0.70 7 8 0.65 8 9 0.60 9 …
biyazelnut
  • 256
  • 7
2
votes
2 answers

Python Pandas Dataframe By Column and Row Value

I have a pandas dataframe with multiple column values that are the same. I'd like to select the series in the column where I pass it the column name, and then they have a unique identifier in one of the index/rows. My data frame looks like this: …
2
votes
3 answers

Get the top 2 values for each unique value in another column

I have a DataFrame like this: student marks term steve 55 1 jordan 66 2 steve 53 1 alan 74 2 jordan 99 1 steve 81 2 alan 78 1 alan …
Let'sbecool
  • 104
  • 6
2
votes
1 answer

pandas loc with multiple or conditions

Suppose I have a following dataframe: x = pd.DataFrame( { 'A': np.random.normal(0, 1, 100), 'B': np.random.normal(0, 1, 100), 'C': np.random.normal(0, 1, 100), 'D': np.random.normal(0, 1, 100), 'E':…
monte
  • 1,482
  • 1
  • 10
  • 26
2
votes
2 answers

Is there a way to pass a string variable into python .loc/.iloc?

Im trying to use loc to get a subset of rows in a dataframe on a condition, but i want to take user input to get what this condition is and then feed that in to the loc statement to create the subset of rows. Ive tried many ways but i dont think loc…
2
votes
1 answer

Set values in a column based on the values of other columns as a group

I have a df that looks something like this: name A B C D 1 bar 1 0 1 1 2 foo 0 0 0 1 3 cat 1 0-1 0 4 pet 0 0 0 1 5 ser 0 0-1 0 6 chet 0 0 0 1 I need to use loc method to add values in a new column ('E') based on the values of the other…
Gus
  • 193
  • 9
2
votes
1 answer

pandas loc to check if value contains any of several words or a string

I have a pandas DataFrame with a column containing strings. (I take the example from Check if string is in a pandas dataframe) import pandas as pd BabyDataSet = [ ('Bob and martin and Andrew', 968), ('Jessica and julia and anthony', 155), …
JFerro
  • 3,203
  • 7
  • 35
  • 88
2
votes
1 answer

Python Dataframe to Columnar format for accessing the columns dynamically

Dataframe data will be : COL1 COL2 COL3 100 200 300 101 201 301 102 202 302 103 203 303 Expected output: Format as HBase (Columnar fashion) Consider COL1 as KEY_COLUMN…
Priyan
  • 21
  • 2
2
votes
1 answer

Filtering pandas dataframe on multiple conditions using loc returns empty dataframe

I am trying to filter my dataframe on multiple conditions using loc The data is coming from an Excel file that I imported into the dataframe using df = pd.read_csv() Here is sample structure from the df using df.head() : df.head() and I need to…
ghalvorson
  • 23
  • 4
2
votes
1 answer

Can't insert a list into a dataframe because of a 'date' column?

I'm working with this dataframe: P Q date 0 [2, 3, 4] [2, 2] 2019-3-18 1 [fff] [2] 2019-3-18 2 [] [2] 2019-10-24 3 [4, 5] [2] 2019-1-1 I can easily transform any list, no matter how many…
2
votes
2 answers

Pandas Python How to make 2 values with equal len?

Hi anyone can help on this? i got error when i run this: conditions5 = [ (data1['Payment Mode'] == '03') ] choices5 = data1['Value Date'] data1['Valid From Date'] = np.select(conditions5, choices5, default ='') data1 Error: List of cases…
Amy Diana
  • 129
  • 1
  • 5
2
votes
0 answers

Why does df.loc[] return the same row twice, when there is only one?

I am currently working with a few Stock Price csv files but there is some strange behaviour of the .loc[] command after using pd.read_csv to get the data into a df. I am doing this with 10 files but I wanted to do a lot more before i ran into this…
Stryder
  • 848
  • 6
  • 9
1 2
3
25 26