Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas slicing from one column to another

# slice from 'foo' to 'cat' by every 2nd column
df.loc[:, 'foo':'cat':2]
# foo quz cat

# slice from the beginning to 'bar'
df.loc[:, :'bar']
# foo bar

# slice from 'quz' to the end by 3
df.loc[:, 'quz'::3]
# quz sat

# attempt from 'sat' to 'bar'
df.loc[:, 'sat':'bar']
# no columns returned

# slice from 'sat' to 'bar'
df.loc[:, 'sat':'bar':-1]
sat cat ant quz bar

# slice notation is syntatic sugar for the slice function
# slice from 'quz' to the end by 2 with slice function
df.loc[:, slice('quz',None, 2)]
# quz cat dat

# select specific columns with a list
# select columns foo, bar and dat
df.loc[:, ['foo','bar','dat']]
# foo bar dat
Comment

PREVIOUS NEXT
Code Example
Python :: python print boolean 
Python :: python check if two sets intersect 
Python :: import pil pycharm 
Python :: genrate unique key in python 
Python :: tkinter yes no dialogue box 
Python :: change colorbar size and place python 
Python :: how to read excel with multiple pages on pandas 
Python :: Issue TypeError: can’t multiply sequence by non-int of type str 
Python :: how to read then overwrite a file with python 
Python :: how to unique list in python 
Python :: radix sort python 
Python :: selection sort python 
Python :: how to check if a cell is empty in openpyxl 
Python :: cant install tensorflow pip python 3.6 
Python :: python recursive sum of digit 
Python :: python writelines 
Python :: change size of plot python 
Python :: how to fetch all chars of a string before a space in python 
Python :: extend tuple python 
Python :: convert dict to string python 
Python :: pandas read_csv column names 
Python :: make entry bigger in tkinter python 
Python :: python depth first search 
Python :: import spacy nlp = spacy.load("ar_core_web_sm") 
Python :: how to find which 2 rows of a df are the most similar 
Python :: basic tkinter gui 
Python :: pandas count rows in column 
Python :: heroku python version 
Python :: merge two series by index 
Python :: formatting in python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =