Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas apply function on two columns

def stream_half(col1, col2):
    if col1 == 'desktop':
        return col2/2
    else:
        return int(col2)
        
df['clean'] = df.apply(lambda row: stream_half(row['device'], 
  row['streams']), axis = 1) 
Comment

pandas pass two columns to function

#Method 1:
df["Delivery Charges"] = df[["Weight", "Package Size", "Delivery Mode"]].apply(
  lambda x : calculate_rate(*x), axis=1)

#Method 2:
df["Delivery Charges"] = df.apply(
  lambda x : calculate_rate(x["Weight"], 
  x["Package Size"], x["Delivery Mode"]), axis=1)
Comment

apply on dataframe access multiple columns

df['col_3'] = df.apply(lambda x: x.col_1 + x.col_2, axis=1)
Comment

how to create multiple columns after applying a function in pandas column python

>>> df = pd.DataFrame([[i] for i in range(10)], columns=['num'])
>>> df
    num
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9

>>> def powers(x):
>>>     return x, x**2, x**3, x**4, x**5, x**6

>>> df['p1'], df['p2'], df['p3'], df['p4'], df['p5'], df['p6'] = 
>>>     zip(*df['num'].map(powers))

>>> df
        num     p1      p2      p3      p4      p5      p6
0       0       0       0       0       0       0       0
1       1       1       1       1       1       1       1
2       2       2       4       8       16      32      64
3       3       3       9       27      81      243     729
4       4       4       16      64      256     1024    4096
5       5       5       25      125     625     3125    15625
6       6       6       36      216     1296    7776    46656
7       7       7       49      343     2401    16807   117649
8       8       8       64      512     4096    32768   262144
9       9       9       81      729     6561    59049   531441
Comment

pandas operations with multiple columns

df.query('A in @mylist')
Comment

how to apply a function to multiple columns in pandas dataframe

>>> print df
   A  B  C
0 -1  0  0
1 -4  3 -1
2 -1  0  2
3  0  3  2
4  1 -1  0
>>> print df.applymap(lambda x: x>1)
       A      B      C
0  False  False  False
1  False   True  False
2  False  False   True
3  False   True   True
4  False  False  False
Comment

PREVIOUS NEXT
Code Example
Python :: MovieWriter stderr: ffmpeg: error while loading shared libraries: libopenh264.so.5: cannot open shared object file: No such file or directory 
Python :: factorial in python 
Python :: django template for loop 
Python :: replace all missing value with mean pandas 
Python :: django __str__ self multiple 
Python :: django logout user 
Python :: genrate unique key in python 
Python :: numpy add one column 
Python :: find the factorial of a given integer in python 
Python :: python reverse words in string 
Python :: 2 distinct numbers random number generator python 
Python :: sklearn cross_val_score scoring metric 
Python :: how to use the print function in python 
Python :: start virtual environment python 
Python :: select rows where column value is in list of values 
Python :: multiple values in python loop for x,y 
Python :: add two datetime python 
Python :: reverse an array python 
Python :: how to fetch all chars of a string before a space in python 
Python :: difference between generator and iterator in python 
Python :: qlistwidget item clicked event pyqt 
Python :: distplot with plotly 
Python :: flask flash not working 
Python :: how can i make a list of leftovers that are str to make them int in python 
Python :: enum python 
Python :: rnadom number python 
Python :: how to save a python object in a file 
Python :: random string generate python of 2.7 
Python :: Save a Dictionary to File in Python Using the dump Function of the pickle Module 
Python :: calculate days between two dates python 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =