How to read the column using Pandas [duplicate] - python

This question already has answers here:
Pandas column access w/column names containing spaces
(6 answers)
Closed 10 months ago.
I am trying to put a condition to print the revenue which is greater or equal to certain number using Pandas in Python.
I am using the code line" df[df.Total Revenue>=6678690.38]"
I am getting the error "SyntaxError: invalid syntax." beacuse its not taking my column name(Total Revenue) with space. So how to read column if the column header is with space?
Note = df is where I am reading my file

this should work
df[df['Total Revenue']>=6678690.38]

Related

syntax for data frames to use for 2 options [duplicate]

This question already has answers here:
How to use str.contains() with multiple expressions in pandas dataframes
(3 answers)
Filter pandas DataFrame by substring criteria
(17 answers)
How to test if a string contains one of the substrings in a list, in pandas?
(4 answers)
Pandas filtering for multiple substrings in series
(3 answers)
Closed 10 months ago.
I have a dataframe, df. One of the column is Text. I want to search the dataframe where the text contains ABC.
Hence, I write the code:
df["Text"].str.contains("ABC")
Now, I want to search which text contains ABC or XYZ.
What will be the syntax?
Using the | pipe is what you need
DF['Text'].str.contains('ABC|XYZ')
DF['Text'].str.contains('ABC') | DF['Text'].str.contains('XYZ')

Split a column value based on condition [duplicate]

This question already has answers here:
Split Pandas Series into DataFrame by delimiter
(2 answers)
Closed last year.
I am trying to split a column based on whether a slash('/) is present in the cell within that column. Not all cells contain slashes. Most only contain 3 letters (e.g.'ABC').
I am trying to avoid for loops since they affect performance. I have tried the following code:
df.column.split('/',expand=True)
I get the following outut:
AttributeError: 'Series' object has no attribute 'split'
Almost there:
df.column.str.split('/',expand=True)

findall string that starts with letter "CU" and return full string [duplicate]

This question already has answers here:
pandas select from Dataframe using startswith
(5 answers)
Closed 3 years ago.
It seems like straight forward thing however could not find appropriate SO answer.
I have a column called title which contain strings. I want to find out rows that starts with letter "CU".
I've tried using df.loc however It's giving me indexError,
Using regex, re.findall(r'^CU', string)
returns 'CU' instead of full name ex: 'CU abcd'. How can I get full name that starts with 'CU'?
EDIT: SORRY, I did not notice it was a duplicate question, problem solved by reading duplicate question.
You can try:
string.startswith("CU")

How to refer to a variable name with spaces in Python? [duplicate]

This question already has answers here:
Pandas column access w/column names containing spaces
(6 answers)
Closed 6 years ago.
How would I deal with this? The variable "residual sugar" has a space, other than don't use variables with spaces (not my data).
plt.plot(trimmedWine.density, trimmedWine.residual sugar, 'bo', alpha=.25)
I suppose you are referring to pandas data frame column, if so, then trimmedWine['residual sugar'] is what you probably need.

Syntax error in pandas - referring column with space and parenthesis [duplicate]

This question already has answers here:
SyntaxError when accessing column named "class" in pandas DataFrame
(2 answers)
Closed 3 years ago.
one of my csv headers is 'TEMPERATURE (C)' (without the ' of-curse)
and i'm trying to do this kind of command:
df1['Average Temp'] = df.'TEMPERATURE (C)'.resample('H', how='mean')
and getting invalid syntax error.
What is the problem?
Shouldn't it be like this?
df1['Average Temp'] = df['TEMPERATURE (C)'].resample('H', how='mean')

Categories