Search
 
SCRIPT & CODE EXAMPLE
 

R

how to drop a column by name in pandas

>>> df.drop(columns=['B', 'C'])
   A   D
0  0   3
1  4   7
2  8  11
Comment

pandas delete column by name

df = df.drop('column_name', axis=1)
Comment

remove name of a column

M
      a b c
1[1,] 1 4 7
2[2,] 2 5 8
3[3,] 3 6 9

row.names(M)<- NULL ; colnames(M)<- NULL
M

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
Comment

how to drop a column by name in pandas

>>> midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
...                              ['speed', 'weight', 'length']],
...                      codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
...                             [0, 1, 2, 0, 1, 2, 0, 1, 2]])
>>> df = pd.DataFrame(index=midx, columns=['big', 'small'],
...                   data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
...                         [250, 150], [1.5, 0.8], [320, 250],
...                         [1, 0.8], [0.3, 0.2]])
>>> df
                big     small
lama    speed   45.0    30.0
        weight  200.0   100.0
        length  1.5     1.0
cow     speed   30.0    20.0
        weight  250.0   150.0
        length  1.5     0.8
falcon  speed   320.0   250.0
        weight  1.0     0.8
        length  0.3     0.2
Comment

drop columns by name

import pandas as pd

# create a sample dataframe
data = {
    'A': ['a1', 'a2', 'a3'],
    'B': ['b1', 'b2', 'b3'],
    'C': ['c1', 'c2', 'c3'],
    'D': ['d1', 'd2', 'd3']
}

df = pd.DataFrame(data)

# print the dataframe
print("Original Dataframe:
")
print(df)

# remove column C
df = df.drop('C', axis=1)

print("
After dropping C:
")
print(df)
Comment

PREVIOUS NEXT
Code Example
R :: empty environment r 
R :: extract attribute in r 
R :: read.table tab separator 
R :: generate pair with one same variable in r 
R :: R create sequence of date each quarters 
R :: number of days in a data set in r 
R :: formatc in r 
R :: no redundant combination expand grid 
R :: r read.csv tab delimited 
R :: switch variable position in r dplyr 
R :: extract residual standard error from lm in r 
R :: excel date format r 
R :: how to count the number of non NA values in R 
R :: next element in a loop if error in r 
R :: digits in format and formatc in r 
Rust :: rust lang sleep 
Rust :: array as a parameter rust 
Rust :: rust loop n times 
Rust :: ..= in rust 
Rust :: rust option 
Rust :: find smallest number in vec rust 
Rust :: where in Rust 
Rust :: rust lang expected item found let 
Rust :: armanriazi•rust•oop 
Rust :: armanriazi•rust•trait•bound 
Rust :: armanriazi•rust•thread•spawning 
Rust :: rust tuple vs vec 
Lua :: lua not equal 
Lua :: luau debounce 
Lua :: roblox brick color 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =