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

select multiple columns in pandas dataframe

df1 = df[['a', 'b']]
Comment

apply on dataframe access multiple columns

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

pandas select multiple columns

#Example, in a df with about 20 columns
#If you want to select columns 1-3, 7, 8, 12-15
# Use numpy's np.r_ to slice the columns and parse it into pandas iloc[] slicer

df.iloc[:, np.r_[1:3, 7, 8, 12:15]]
#This selects all rows "df.iloc[:," and then these selected columns "np.r_[1:3, 7, 8, 12:15]]"
#Remember to import numpy ;)
Comment

pandas assign multiple columns at once

df = pd.DataFrame({'col1':[0,1,2], 'col2':[0,1,2], 'col3':[0,1,2]})

df
   col1  col2  col3
0     0     0     0
1     1     1     1
2     2     2     2

df['col1'], df['col2'], df['col3'] = zip(*df.apply(lambda r: (1, 2, 3), axis=1))

df
   col1  col2  col3
0     1     2     3
1     1     2     3
2     1     2     3
Comment

assign multiple columns pandas

import pandas as pd

df = {'col_1': [0, 1, 2, 3],
        'col_2': [4, 5, 6, 7]}
df = pd.DataFrame(df)

df[[ 'column_new_1', 'column_new_2','column_new_3']] = [np.nan, 'dogs',3]  #thought this wo
Comment

Apply multiple columns

def f(x):
    d = {}
    d['a_sum'] = x['a'].sum()
    d['a_max'] = x['a'].max()
    d['b_mean'] = x['b'].mean()
    d['c_d_prodsum'] = (x['c'] * x['d']).sum()
    return pd.Series(d, index=['a_sum', 'a_max', 'b_mean', 'c_d_prodsum'])

df.groupby('group').apply(f)

         a_sum     a_max    b_mean  c_d_prodsum
group                                           
0      0.864569  0.446069  0.466054     0.173711
1      1.478872  0.843026  0.687672     0.630494
Comment

df multiple columns into one column

df.stack().reset_index()

   level_0   level_1  0
0        0  Column 1  A
1        0  Column 2  E
2        1  Column 1  B
3        1  Column 2  F
4        2  Column 1  C
5        2  Column 2  G
6        3  Column 1  D
7        3  Column 2  H
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 assign multiple columns

In [1069]: df.assign(**{'col_new_1': np.nan, 'col2_new_2': 'dogs', 'col3_new_3': 3})
Out[1069]:
   col_1  col_2 col2_new_2  col3_new_3  col_new_1
0      0      4       dogs           3        NaN
1      1      5       dogs           3        NaN
2      2      6       dogs           3        NaN
3      3      7       dogs           3        NaN
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 :: find length of string in python 
Python :: how to declare np datetime 
Python :: tf MaxPooling2D 
Python :: how to get user input python 
Python :: Random night stars with python turtle 
Python :: random.choices in python 
Python :: python enumerate for loop 
Python :: python new date 
Python :: python error handling 
Python :: polish notation python 
Python :: pandas show full columns 
Python :: sequenza di fibonacci python 
Python :: find total no of true in a list in python 
Python :: what is tensor in deep learning 
Python :: count how much a number is in an array python 
Python :: python swarm plot seaborn 
Python :: how to add space in st.write streamlit 
Python :: 2d array in python 
Python :: how to return a value from a function in python 
Python :: python turn off garbage collection 
Python :: how to return a missing element in python 
Python :: windows how to store filepath as variabley python 
Python :: yield python 
Python :: display multiple dataframe as table jupyter notebook 
Python :: python remove one element from numpy array 
Python :: remove  python 
Python :: weighted average in python pandas 
Python :: slice in python 
Python :: python check if input() gives error 
Python :: count no of nan in a 2d array python 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =