What about doing
>>> df['C'] = np.where(df.B>0., df.A/df.B, 0.)
which reads as :
where
df.B
is strictly positive, returndf.A/df.B
, otherwise return0.
More Related Contents:
- How to change the order of DataFrame columns?
- Split pandas dataframe based on groupby
- Add missing dates to pandas dataframe
- Fast punctuation removal with pandas
- pandas loc vs. iloc vs. at vs. iat?
- Pandas: sum DataFrame rows for given columns
- Skip rows during csv import pandas
- How to check if any value is NaN in a Pandas DataFrame
- How to drop rows from pandas data frame that contains a particular string in a particular column? [duplicate]
- pandas dataframe groupby datetime month
- Find the end of the month of a Pandas DataFrame Series
- vlookup in Pandas using join
- Vectorised Haversine formula with a pandas dataframe
- Read a small random sample from a big CSV file into a Python data frame
- How to concatenate two dataframes without duplicates?
- Most efficient way to forward-fill NaN values in numpy array
- Memory error when using pandas read_csv
- Python Pandas replicate rows in dataframe
- Efficiently create sparse pivot tables in pandas?
- Can’t drop NAN with dropna in pandas
- Legend only shows one label when plotting with pandas
- How to remove or hide x-axis labels from a seaborn / matplotlib plot
- Computing np.diff in Pandas after using groupby leads to unexpected result
- Remove NaN ‘Cells’ without dropping the entire ROW (Pandas,Python3)
- How to read a list of parquet files from S3 as a pandas dataframe using pyarrow?
- Error: The truth value of a Series is ambiguous – Python pandas
- Is it possible to add a string as a legend item in matplotlib
- Pivot Tables of Counts in Pandas DataFrame
- append dictionary to data frame
- ValueError: Shape of passed values is (1, 6), indices imply (6, 6)