logo
down
shadow

Pandas: np.where with multiple conditions on dataframes


Pandas: np.where with multiple conditions on dataframes

By : praveen
Date : November 22 2020, 10:56 AM
will help you It is not clear to me what you exactly want to do when a y element is equal to zero... anyway the key point in this answer is "use np.logical_{and,not,or,xor} functions".
I think that the following, albeit formulated differently from your example, does what you want, but if I'm wrong you should be able to combine different tests to achieve what you want,
code :
x = np.where(np.logical_or(x*y>0, y==0), x, 0)


Share : facebook icon twitter icon
Boolean Indexing in Pandas Dataframes with multiple conditions

Boolean Indexing in Pandas Dataframes with multiple conditions


By : Danny Park
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to drop specific rows from my 3-column dataframe based on values in two of the columns. I have been trying to use Boolean indexing, but have not been seeing the results I expect.
code :
DF[(DF['SchoolID'] != 1234) & (DF['State'] != 'New York')]
DF[(DF['SchoolID'] == 1234) & (DF['State'] == 'New York')]
DF[(DF['SchoolID'] != 1234) | (DF['State'] != 'New York')]
Merging pandas dataframes on multiple conditions (python/pandas)

Merging pandas dataframes on multiple conditions (python/pandas)


By : Dhavan Sanghvi
Date : March 29 2020, 07:55 AM
To fix this issue One way is to do the plain old merge then throw away the values out of the range:
code :
In [11]: df3 = df1.merge(df2)

In [12]: df3
Out[12]:
   ID1  Chr   pos ID2  pos-start  pos-end
0    a   12   500   x        200      400
1    b   12   250   x        200      400
2    c   12   300   x        200      400
3    d   16  2000   y       1000     1100
4    d   16  2000   z       1070     1200
5    e   16  1050   y       1000     1100
6    e   16  1050   z       1070     1200
7    f   16  1075   y       1000     1100
8    f   16  1075   z       1070     1200
9    d   16  1150   y       1000     1100
10   d   16  1150   z       1070     1200

In [13]: df3[(df3["pos-start"] < df3["pos"]) & (df3["pos"] < df3["pos-end"])]
Out[13]:
   ID1  Chr   pos ID2  pos-start  pos-end
1    b   12   250   x        200      400
2    c   12   300   x        200      400
5    e   16  1050   y       1000     1100
7    f   16  1075   y       1000     1100
8    f   16  1075   z       1070     1200
10   d   16  1150   z       1070     1200
In [14]: df3[(df3["pos-start"] < df3["pos"]) & (df3["pos"] < df3["pos-end"])][['ID2', 'ID1', 'Chr', 'pos']]
Out[14]:
   ID2 ID1  Chr   pos
1    x   b   12   250
2    x   c   12   300
5    y   e   16  1050
7    y   f   16  1075
8    z   f   16  1075
10   z   d   16  1150
Creating a new column based on selecting by multiple conditions between two pandas dataframes

Creating a new column based on selecting by multiple conditions between two pandas dataframes


By : Robert Joy
Date : March 29 2020, 07:55 AM
wish of those help I have two dataframes that contain (some) common columns (A,B,C), but are ordered differently and have different values for C.
code :
merge_df = pd.merge(df1, df2, on=['A', 'B'])
df1['C'] = merge_df['C_y']
Joining two pandas dataframes based on multiple conditions

Joining two pandas dataframes based on multiple conditions


By : 10skitter
Date : March 29 2020, 07:55 AM
around this issue df_a and df_b are two dataframes that looks like following , You need an inner merge, specifying both merge columns in each case:
code :
res = df_a.merge(df_b, how='inner', left_on=['A', 'B'], right_on=['A', 'B_new'])

print(res)

    A       B    C    D    E   B_new    F
0  x1   Apple  0.3  0.9  0.6   Apple  0.3
1  x1  Orange  0.1  0.5  0.2  Orange  0.1
2  x2   Apple  0.2  0.2  0.1   Apple  0.2
3  x2  Orange  0.3  0.4  0.9  Orange  0.3
4  x2   Mango  0.1  0.2  0.3   Mango  0.1
5  x3  Orange  0.3  0.1  0.2  Orange  0.3
Multiple Conditions Sum Between Two Pandas Dataframes

Multiple Conditions Sum Between Two Pandas Dataframes


By : Noor Shodiqin
Date : October 05 2020, 03:00 AM
With these it helps Using merge, query, GroupBy.sum:
code :
mrg = df1.merge(df2, left_on='Product1', right_on='Product2')

mrg.query('Date2 <= Date1').groupby(['Product1', 'Date1'], as_index=False)['Payment'].sum()
  Product1      Date1  Payment
0        A 2019-02-01      250
1        A 2019-12-15      350
2        B 2019-03-01       50
Related Posts Related Posts :
  • ModuleNotFoundError: No module named 'users'
  • Interpolating with multiple y-values
  • Import warning PACKAGE.egg is added to sys.path
  • Is there a key for the default namespace when creating dictionary for use with xml.etree.ElementTree.findall() in Python
  • Using fill_between() with a Pandas Data Series
  • How to build a lookup table for tri-linear interpolation in NumPy?
  • Matrix vector multiplication along array axes
  • Can a cookiejar object be pickled?
  • __init__.py in project folder breaks nose tests
  • Comparing times with sub-second accuracy
  • advanced search using HayStack + Solr in Django?
  • Base test case class for python unittest
  • The PyData Ecosystem
  • Finding unique entries with oldest time stamp
  • Custom filesize format with Python Humanize?
  • Use `tf.image.resize_image_with_crop_or_pad` to resize numpy array
  • Sum number of occurences of string per row
  • Calculating 'Diagonal Distance' in 3 dimensions for A* path-finding heuristic
  • porting PyGST app to GStreamer1.0 + PyGI
  • Connection refused in Tornado test
  • How much time does take train SVM classifier?
  • Turning a string into list of positive and negative numbers
  • Python lists get specific length of elements from index
  • python.exe version 3.3.2 64 & 32 crash while creating .exe file on win 7 64 & 32 with cx_Freeze
  • Efficient nearest neighbour search for sparse matrices
  • django filter_horizontal can't display
  • How to install FLANN and pyflann on Windows
  • How can I plot the same figure standalone and in a subplot in Matplotlib?
  • read-only cells in ipython notebook
  • filling text file with dates
  • error:AttributeError: 'super' object has no attribute 'db_type' when run "python manage.py syncdb" in django
  • python imblearn make_pipeline TypeError: Last step of Pipeline should implement fit
  • Write to csv: columns are shifted when item in row is empty (Python)
  • DuckDuckGo search returns 'List Index out of range'
  • Python function which can transverse a nested list and print out each element
  • Python installing xlwt module error
  • Python mayavi: Adding points to a 3d scatter plot
  • Making a basic web scraper in Python with only built in libraries - Python
  • How to calculate the angle of the sun above the horizon using pyEphem
  • Fix newlines when writing UTF-8 to Text file in python
  • How to convert backward slash command in python to run on Linux
  • PyCharm Code Inspection doesn't include PEP 8
  • How can I use Python namedtuples to insert rows into mySQL database
  • Increase / Decrease Mac Address in Python from String
  • Scrollable QLabel image in PyQt5
  • (Python 2.7) Access variable from class with accessor/mutator
  • Why does "from [Module] import [Something]" takes more time than "import [Module"
  • jira python oauth: how to get the parameters for authentication?
  • Python - How to specify a relative path by jumping a subdirectory?
  • Extract scientific number from string
  • Scrapy: Python cannot find the spider
  • get the values in a given radius from numpy array
  • Is it possible to duplicate a pipe in Python, so that it has one write end but two read ends?
  • Why does wget use Firefox cookies to login on an authenticated webpage?
  • python import behaviour: different objects from same file?
  • Create YoY Graph with Matplotlib
  • Safe use of eval() or alternatives - python
  • Unix change desktop background seamlessly
  • Profiling Python code that uses multiprocessing?
  • How to query a database after render_template
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org