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

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.

Related

How to read the column using Pandas [duplicate]

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]

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')

How to can I get the opposite values to between? [duplicate]

This question already has answers here:
How can I obtain the element-wise logical NOT of a pandas Series?
(6 answers)
Closed 3 years ago.
Hi I am trying to get the opposite values to between
I get a few data of this way:
x[x.between(x.quantile(0.25), x.quantile(0.75))]
But I need the opposite data, how can get it?
Thanks
You can use the ~ to negate.
x[~x.between(x.quantile(0.25), x.quantile(0.75))]

Manipulate variables in Python [duplicate]

This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.

List of strings. New variables with those name (without classes). Python [duplicate]

This question already has answers here:
How can you dynamically create variables? [duplicate]
(8 answers)
Closed 8 years ago.
if one had a list of strings, how could they create variables (empty list objects) with those names?
pre_vars= ['list_1','list_2','list_3']
print list_1,list_2,list_3
>>> [],[],[]
I saw some examples that were similar but they were using classes. Can this be done without using classes?
Use globals():
for name in pre_vars:
globals()[name]= []

Categories