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

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

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]

String replace() vs string translate() method [duplicate]

This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?

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

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.

Convert DataFrame into dict [duplicate]

This question already has answers here:
Pandas: Convert dataframe to dict of lists
(2 answers)
Closed 6 years ago.
I use pandas to read df.csv, so I have a Dataframe Like this,
I want to convert it to dict like this
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html
try :
df.to_dict(orient='list')

Categories