조회수 : 2395
dropping rows and columns in pandas (판다스에서 행 또는 열 삭제하기)

pandas drop row(s) or column(s)

In [74]:
 
import pandas as pd
df = pd.read_clipboard()
df.head(5)
Out[74]:
Name Position Office Age Start date Salary
0 Name Position Office Age Start date Salary
1 Airi Satou Accountant Tokyo 33 2008/11/28 $162,700
2 Angelica Ramos Chief Executive Officer (CEO) London 47 2009/10/09 $1,200,000
3 Ashton Cox Junior Technical Author San Francisco 66 2009/01/12 $86,000
4 Bradley Greer Software Engineer London 41 2012/10/13 $132,000
In [75]:
 
# df.drop(df['Name'] == "Name", inplace=True)
df = df[1:]
df.head(3)
Out[75]:
Name Position Office Age Start date Salary
1 Airi Satou Accountant Tokyo 33 2008/11/28 $162,700
2 Angelica Ramos Chief Executive Officer (CEO) London 47 2009/10/09 $1,200,000
3 Ashton Cox Junior Technical Author San Francisco 66 2009/01/12 $86,000
In [76]:
 
df.drop('Position', axis = 1)
df.drop(['Name','Office'], axis=1)
df.head(2)
Out[76]:
Name Position Office Age Start date Salary
1 Airi Satou Accountant Tokyo 33 2008/11/28 $162,700
2 Angelica Ramos Chief Executive Officer (CEO) London 47 2009/10/09 $1,200,000
In [77]:
 
df.drop(1, inplace=True)
df.head(3)
Out[77]:
Name Position Office Age Start date Salary
2 Angelica Ramos Chief Executive Officer (CEO) London 47 2009/10/09 $1,200,000
3 Ashton Cox Junior Technical Author San Francisco 66 2009/01/12 $86,000
4 Bradley Greer Software Engineer London 41 2012/10/13 $132,000
In [78]:
 
df.drop([2,4],inplace=True)
df.head(3)
Out[78]:
Name Position Office Age Start date Salary
3 Ashton Cox Junior Technical Author San Francisco 66 2009/01/12 $86,000
5 Brenden Wagner Software Engineer San Francisco 28 2011/06/07 $206,850
6 Brielle Williamson Integration Specialist New York 61 2012/12/02 $372,000
In [80]:
 
df = df[:-2]
df.head()
Out[80]:
Name Position Office Age Start date Salary
3 Ashton Cox Junior Technical Author San Francisco 66 2009/01/12 $86,000


상담을 원하시면 상담신청을 눌러 주세요

다른 포스팅 Tag Cloud