391 questions
0
votes
1
answer
397
views
Python: Dataframe starting from specific date [duplicate]
Using python, how can I take the values from 2021-09-13 to 2021-09-16?
I tried the following:
date_start = df['date'].iloc[-4] #2021-09-13
date_end = df['date'].iloc[-1] #2021-09-16
df = df.loc[...
0
votes
0
answers
55
views
Python Dataframe loc : KeyError [duplicate]
df = pd.read_excel(excelName)
df.columns=df.columns.str.strip()
print(df.columns.tolist())
# print result is ['序号', '学号', '姓名', '文件位置', '邮箱地址']
print(df.loc['文件位置'])
It seems I have ...
4
votes
3
answers
3k
views
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-...
0
votes
1
answer
349
views
pandas/python: combining replace and loc for replacing part of column names within a range
is it possible to use loc and replace functions to replace part of the column name for a range of columns? I've tried combining the replace and loc functions in a couple of variation, however, was ...
1
vote
1
answer
987
views
How to assign a list element with loc function in pandas df?
I have a question regarding the loc function for my pandas DataFrame.
First I want to check if the person is a student, then I would like to assign to first value of the list 'Course' for that ...
1
vote
3
answers
166
views
.loc function for a specific label returning an empty data frame?
For context, I'm trying to filter out rows in my dataframe that only belong to the year 2021.
This is my script code:
test = all_SS_batting_columns.loc[all_SS_batting_columns['Year'] == '2021']
but it ...
4
votes
1
answer
5k
views
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.
...
-1
votes
1
answer
67
views
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
-------...
-1
votes
1
answer
44
views
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',...
0
votes
2
answers
81
views
Error while trying to define a function to create price bucket for apps
I have an csv data set that I imported in Jupyter and stored under inp0.
I'm trying to create price bucket for these using .loc function in pandas bet getting below error.
My Code:
inp0.loc[inp0.price=...
0
votes
1
answer
1k
views
using .loc to assign values from another dataframe by index returns : ValueError: Must have equal len keys and value when setting with an iterable
df_state_group:
Index
State
assigned_airport
4
Florida
NaN
5
Florida
NaN
6
Florida
NaN
7
Florida
NaN
df1:
Index
State
assigned_airport
0
Washington
NaN
1
New York
NaN
2
Illinois
NaN
3
Texas
NaN
4
...
-2
votes
1
answer
376
views
python iloc errors new column data frame [duplicate]
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 ...
0
votes
2
answers
77
views
Switch color of sns.swarnplot
Does anybody know, if its possible to switch the colors, so that i can distinguish every row instead of every column ? And how do I add in a legend, where i can see which player (one color for each ...
1
vote
1
answer
2k
views
LOC search string with AND condition in Python
I'm trying to use LOC with an AND condition. It works fine with OR conditions, but I can't get it to work with ANDs when there are duplicate values in a column.
def locreplace(df,col,needle,replace,...
1
vote
1
answer
182
views
Pandas loc with prefix
I have two data frames and I want for each line in one data frame to locate the matching line in the other data frame by a certain column (containing some id). I thought to go over the lines in the ...
2
votes
1
answer
239
views
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 vaue
KEY_COLUMN
KEY
VALUE
100
...
0
votes
1
answer
159
views
store values after groupby and loc
using df to select two columns col_1 and col_2 after groupby and loc and store at variable a in dataframe format
Python Code
import pandas as pd
import numpy as np
df = pd.DataFrame(dict(Gender=['M', ...
1
vote
0
answers
31
views
what is the difference between these .loc() codes [duplicate]
I am wondering what is the difference between these two lines:
data.loc[(data['MINIMUM_PAYMENTS'].isnull()==True)]['MINIMUM_PAYMENTS']
data.loc[(data['MINIMUM_PAYMENTS'].isnull()==True),'...
1
vote
2
answers
57
views
Replacing values in a df between certain values (replace >1 to 4 with 1)
I would like to replace certain value-thresholds in a df with another value.
For example all values between 1 and <3.3 should be summarized as 1.
After that all values between >=3.3 and <10 ...
1
vote
0
answers
94
views
DataFrame filtering: .loc vs not
Can someone elaborate on the difference between using .loc and not in the examples below? What is the benefit of using:
rslt_df = dataframe[dataframe['Percentage'] > 70], as opposed to
rslt_df = ...
0
votes
1
answer
666
views
Pandas - fill a new column with a single value, based on .loc value within dataframe
I have a DataFrame with a list of returns data. One row in the DataFrame contains the return for the benchmark, 'ASX', which in this example has a value of 6. (Values are updated using market data, so ...
-1
votes
2
answers
100
views
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 | ...
0
votes
1
answer
984
views
python: loop through list of dataframes and list of lists
I have a list of dataframes (lst_dfs) of which i want to subset columns using a list of partial column names (lst). The list of the columns needs to be applied with startswith as sometimes the end of ...
0
votes
4
answers
51
views
Get the minimum for each data in the data frame
I have this data frame:
data = {name: ['a', 'a','b', 'c', 'd', 'b', 'b', 'a', 'c'],
number: [32, 25, 9 , 43,8, 5, 11, 21, 0]
}
and I want to get min number for each name where data in the ...
6
votes
2
answers
5k
views
Change value in pandas after chained loc and iloc
I have the following problem: in a df, I want to select specific rows and a specific column and in this selection take the first n elements and assign a new value to them. Naively, I thought that the ...
0
votes
2
answers
179
views
Pandas - row selection with column operation & .loc[]
I have been practicing with pandas and have the following problem. I have been practicing with an Olympic medal data set where the countries are listed in one column, and the medals in subsequent ...
1
vote
1
answer
654
views
Why does .loc not always match column names?
I noticed this today and wanted to ask because I am a little confused about this.
Lets say we have two df's
df = pd.DataFrame(np.random.randint(0,9,size=(5,3)),columns = list('ABC'))
A B C
0 ...
1
vote
1
answer
2k
views
Filtering data using Pandas .loc function
I have a df of car miles per gallon data, which are int (as far as I can tell).
Using Pandas, I'm filtering with df.loc[(df['Horsepower'] >90)] which is giving me the results I expect - cars with &...
0
votes
2
answers
792
views
Pandas .loc return some empty arrays so I can't get their values
I'm crossing the information into two tables. I want to get the information from the column Register Number in the table profile and put it into the column Delivers, which I just created, into the ...
-3
votes
3
answers
3k
views
Error: '>' not supported between instances of 'method' and 'int'
This code isn't working in Jupyter IDE. I can't find my mistake. Please help.
The first 5 rows of the dataframe I am using is shown here:
11
votes
2
answers
1k
views
Pandas DataFrame Assignment Bug using Dictionaries of Strings and Floats?
Problem
Pandas seems to support using df.loc to assign a dictionary to a row entry, like the following:
df = pd.DataFrame(columns = ['a','b','c'])
entry = {'a':'test', 'b':1, 'c':float(2)}
df.loc[0] = ...
0
votes
1
answer
576
views
Pandas find data with loc function having special characters
I'm trying to find data in my dataframe which doesn't include this string []
In my dataset I have in column tags these values:
I'm using following code but it doesn't work as I expect
conversion_rate ...
1
vote
3
answers
1k
views
Is there a way to incorporate f strings in pandas loc output
I'm trying to set up a script which will generate comments for student reports. To set up a general template, I'm using a marks table which has their percentage scores. I'm having trouble using an f ...
5
votes
1
answer
4k
views
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 ...
1
vote
1
answer
68
views
Identify number of occurrence grouped by 'Year' - continued
This is a continuation of a previous question.
Now, I want to have these occurrences grouped by year so that the output would become something like:
Combo Occurrence (2017) Occurrence (...
-1
votes
1
answer
226
views
Assigning a new value to a dataframe cell using df.loc
Can someone please tell me what the right syntax would be to do the following:
dfpollution3.loc(max(dfpollution3.index),'newhospinextday') = 10
I get the following error:
dfpollution3.loc(max(...
2
votes
1
answer
2k
views
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 ...
1
vote
1
answer
697
views
Pandas dataframe loop all values for certain column
Working with pandas dataframe suppose i have data frame with similar structure as following:
import pandas as pd
a_choise = ["True", "False", "False", "False", &...
1
vote
0
answers
449
views
.loc with strip() and map()
I everyone, I've gotten the dreaded "A value is trying to be set on a copy of a slice from a DataFrame." error.
I saw a possible solution here
How to deal with SettingWithCopyWarning in ...
1
vote
3
answers
111
views
Locating a row in pandas based on a condition
So this is a common question but I cant find an answer that fits this particular scenario.
So I have a Dataframe with columns for genres eg "Drama, Western" and one hot encoded versions of ...
0
votes
1
answer
521
views
How can I solved KeyError: "None of [Index(['lymphoid_neoplasm'], dtype='object')] are in the [index]"
This code line:
gen_histogram = df.loc[1, ['lymphoid_neoplasm']]
gives a error like KeyError: "None of [Index(['lymphoid_neoplasm'], dtype='object')] are in the [index]"
All code:
import ...
1
vote
1
answer
133
views
A more pythonic way to filter rows across multiple columns based on single value in Python 3.6+
I am looking for help on how to simplify my code. The DataFrame is >100k rows and could have multiple columns that contain a mix of strings and integers. Here is an example df:
data = {
"...
1
vote
0
answers
1k
views
How to copy rows from one dataframe to another dataframe based on the index?
I have a problem with 'loc' and'iloc' .So I have a list and a data frame, I want to check if the data in list and date in data frame of a column is matching. if matching I need to copy it to a new ...
1
vote
1
answer
136
views
Extract information to work with with pandas
I have this dataframe:
Column Non-Null Dtype
0 nombre 74 non-null object
1 fabricante - 74 non-null - object
2 calorias -74 ...
1
vote
1
answer
150
views
replacing values with .loc
I am trying to loop through a DF column and get just the URL key from a column of strings. I have printed every stage of this loop and it works correctly. However when printing the DF after the loop ...
0
votes
2
answers
398
views
How to use loc from pandas?
I have this code to replace ages from numeric data to categorical data. I'm trying to do it that way, but it's not working. Can anybody help me?
for df in treino_teste:
df.loc[df['Age'] <= 13, '...
2
votes
1
answer
185
views
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 ...
0
votes
1
answer
347
views
pandas loc search by part of a string not using regexp [duplicate]
import pandas as pd
data = {'name': ['HelloWorld', 'ByeWorld'],
'physics': [22, 33],
'chemistry': [44, 55]}
a = pd.DataFrame(data)
b = a.loc[a['name'] == 'Hello']
print(b)
This ...
0
votes
0
answers
43
views
pandas loc code to replace column contents no longer working following software update, key error but key is there
I have some code which looks for the full name of a browser in a column and where found overwrites with a short name. this is to allow simplified filtering without version numbers in visualisations.
...
0
votes
2
answers
46
views
Adjust numeric scale for a set of rows in a more effective/Pythonic way?
So my dataset is survey data, where each row shows a question and a unique respondent's numeric response to that question. Unfortunately, the scale was backwards for some question (aka 1s should be 4s ...