logo
down
shadow

conditional dataframe operations using Pandas


conditional dataframe operations using Pandas

By : Gandhi Mrvica
Date : November 21 2020, 09:01 AM
Hope this helps You can apply function f for each group.
Function f sum all values of column C2, because there not depends on value of key2. Values of C1 depends on key2, so there are selected only value with df['key2'] == 'Y'.
code :
print A
#  key1 key2  C1  C2
#0    A    X   5   2
#1    A    Y   3   2
#2    B    X   6   1
#3    B    Y   1   3
#4    C    Y   1   4
#5    D    X   2   3
#6    D    Y   1   3

def f(df):
    df['RESULT'] = df['C2'].sum() + df['C1'].loc[df['key2'] == 'Y'].sum()
    df['RESULT'].loc[df['key2'] == 'X'] = 0
    return df

df = A.groupby('key1', sort = False).apply(f)
print df
#  key1 key2  C1  C2  RESULT
#0    A    X   5   2       0
#1    A    Y   3   2       7
#2    B    X   6   1       0
#3    B    Y   1   3       5
#4    C    Y   1   4       5
#5    D    X   2   3       0
#6    D    Y   1   3       7


Share : facebook icon twitter icon
Conditional operations for rows in pandas dataframe

Conditional operations for rows in pandas dataframe


By : obaid
Date : March 29 2020, 07:55 AM
This might help you You can use loc:
code :
df.loc[df.prediction < 0, 'return'] = 0
print df
    returns  prediction  return
0 -1.005705    0.999999     NaN
1  0.005952    1.000000     NaN
2  0.000000   -0.999891       0
3  0.020000   -1.000000       0
4  0.000000    1.000000     NaN
5  0.005000    1.000000     NaN
6  0.000000   -0.999984       0
7 -0.005813   -0.999871       0
df.loc[df.prediction < 0, 'returns'] = 0
print df
    returns  prediction
0 -1.005705    0.999999
1  0.005952    1.000000
2  0.000000   -0.999891
3  0.000000   -1.000000
4  0.000000    1.000000
5  0.005000    1.000000
6  0.000000   -0.999984
7  0.000000   -0.999871
create a new pandas dataframe by taking values from a different dataframe and perforing some mathematical operations on

create a new pandas dataframe by taking values from a different dataframe and perforing some mathematical operations on


By : James
Date : March 29 2020, 07:55 AM
To fix this issue The general idea is to stack your values so you can apply numpy's fast, vectorized functions.
code :
# stack the dataframe
df2 = df.stack().reset_index(level=1)
df2.columns = ['sec', 'value']
# extract the sector number
df2['sec_no'] = df2['sec'].str.slice(-2).astype(int)

# apply numpy's vectorized functions
import numpy as np
df2['x'] = df2['value'] * (np.cos(np.radians(1.40625*(df2['sec_no']))))
df2['y'] = df2['value'] * (np.sin(np.radians(1.40625*(df2['sec_no']))))
                       sec  value  sec_no         x         y
1970-01-01 05:54:17  sec01   8.50       1  8.497440  0.208600
1970-01-01 05:54:17  sec02   8.62       2  8.609617  0.422963
1970-01-01 05:54:17  sec03   8.53       3  8.506888  0.627506
1970-01-01 05:54:17  sec04   8.45       4  8.409311  0.828245
1970-01-01 05:54:17  sec05   8.50       5  8.436076  1.040491
df2[['sec', 'x', 'y']].pivot(columns='sec')
Pandas dataframe - speed in python: dataframe operations, numba, cython

Pandas dataframe - speed in python: dataframe operations, numba, cython


By : Prateek Vora
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a financial dataset with ~2 million rows. I would like to import it as a pandas dataframe and add additional columns by applying rowwise functions utilizing some of the existing column values. For this purpose I would like to not use any techniques like parallelization, hadoop for python, etc, and so I'm faced with the following: , How about simply:
code :
df.loc[:, 'px'] = (alpha * beta) / df.loc[:, 'time'] * df.loc[:, 'vol']
Vectorized Operations on two Pandas DataFrame to create a new DataFrame

Vectorized Operations on two Pandas DataFrame to create a new DataFrame


By : Ko Ko Naing
Date : October 14 2020, 09:25 AM
it helps some times I have orders.csv as a dataframe called orders_df: , Vectorized Solution
code :
j = np.array([df_trades.columns.get_loc(c) for c in orders_df.Symbol])
i = np.arange(len(df_trades))
o = np.where(orders_df.Order.values == 'BUY', -1, 1)
v = orders_df.Shares.values * o
t = df_trades.values
t[i, j] = v

df_trades.loc[:, 'CASH'] = \
    df_trades.drop('CASH', 1, errors='ignore').mul(prices_df).sum(1)
df_trades

            AAPL  IBM  GOOG  XOM  SPY     CASH
Date                                          
2011-01-10  -100    0     0    0    0 -15000.0
2011-01-13   200    0     0    0    0  50000.0
2011-01-13     0 -100     0    0    0 -30000.0
2011-01-26     0    0   200    0    0  20000.0
Calculate conditional probability using groupby and shift operations in Pandas dataframe

Calculate conditional probability using groupby and shift operations in Pandas dataframe


By : user2816481
Date : March 29 2020, 07:55 AM
This might help you I have a dataframe with patients and their visits and the presence of a disease in their left and/or right eye is labeled with {0,1} values (0 = not present and 1 = present). The dataset looks like this: , IIUC:
code :
df.groupby('L').R.mean()
L
0    0.000000
1    0.384615
Name: R, dtype: float64
df.groupby(['Patient','L']).R.mean()
Patient  L
P_1      1    0.2
P_2      1    0.5
P_3      0    0.0
         1    0.5
Name: R, dtype: float64
Related Posts Related Posts :
  • unable to post file+data using python-requests
  • How to test aws lambda functions locally
  • inconsistent plot between matplotlib and seaborn in Python
  • How matplotlib show obvious changes?
  • Project in Python3, reading files, word data
  • Check for specific Item in list without Iteration or find()
  • Unicode encoding when reading from text file
  • Overloaded variables in python for loops?
  • All elements have same value after appending new element
  • Python Threading loop
  • `_pickle.UnpicklingError: the STRING opcode argument must be quoted`
  • Python: How to stop a variable from exceeding a value?
  • python textblob and text classification
  • Django - Context dictionary for attribute inside a class
  • Database is not updated in Celery task with Flask and SQLAlchemy
  • Shapely intersections vs shapely relationships - inexact?
  • How to extract a percentage column from a periodic column and the sum of the column?
  • Zombie ssh process using python subprocess.Popen
  • Python regex to capture a comma-delimited list of items
  • joining string and long in python
  • Value Error in python numpy
  • Check if any character of a string is uppercase Python
  • TensorFlow - why doesn't this sofmax regression learn anything?
  • Python Anaconda Proxy Setup via .condarc file on Windows
  • Creating django objects from emails
  • Get spotify currently playing track
  • Select multiple columns and remove values according to a list
  • Python - How to Subtract a Variable By 1 Every Second?
  • Tkinter unable to alloc 71867 bytes
  • How to add Variable to JSON Python Django
  • CSRF token missing or invalid Django
  • Python: writing to a text file
  • Extracting multiple rows from pandas dataframe and converting to columns
  • Pinging a remote PC with Flask, causing server to block
  • Making a fractal graph using a 2D array?
  • Replacing a word in a list with a value from a dict
  • Savefig as eps yields a non-usable eps
  • Crispy-forms InlineRadios don't show my model state
  • Getting Title of a text
  • Python with numpy: How to delete an element from each row of a 2-D array according to a specific index
  • Sending and Receive data from a web page - Selenium
  • KeyError with Pyro4
  • Python module error
  • Python :: Attribute in superclass not available in inheriting subclass
  • Why does greater than and unequal operators work even though only less than and equal operator has been overloaded
  • Input length mismatch scikit
  • Print String in decreasing length
  • Overloading the [] operator in python class to refer to a numpy.array data member
  • Sympy - altering the range of the y axis for a plot
  • How do I programmatically list a DLL's dependencies in C++ or Python?
  • How do I lock window resizing in a Python matplotlib window?
  • Word boundary RegEx search using PyMongo
  • Iterating over a string by only changing one element in the string
  • classification of data where attribute values are strings
  • Validate user input using regular expressions
  • Synchronizing and Resampling two timeseries with non-uniform millisecond intraday data
  • determing the number of sentences, words and letters in a text file
  • Deploying impure Python packages to AWS
  • Navigating between multiple Tkinter GUI frames
  • Python - Do I need to remove instances from a dictionary?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co