How to Select Row and Column in Pandas DataFrame
1. Selecting Column From Pandas DataFrame
import pandas as pd
data = { ‘first’ : pd.Series( [ 1,2,3,4,5 ], index= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] ),
‘second’ : pd.Series( [ 10,20,30,40,50 ], index= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] ) }
df=pd.DataFrame(data)
print (df [‘first’])
import pandas as pd
data = { ‘first’ : pd.Series( [ 1,2,3,4,5 ], index= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] ),
‘second’ : pd.Series( [ 10,20,30,40,50 ], index= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] ) }
df=pd.DataFrame(data)
print (df [‘second’])
一一一
a 10
b 20
c 30
d 40
e 50
2. Selecting Row From Pandas DataFrame
import pandas as pd
data = { ‘first’ : pd.Series( [ 1,2,3,4,5 ], index= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] ),
‘second’ : pd.Series( [ 10,20,30,40,50 ], index= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] ) }
df=pd.DataFrame(data)
print (df.loc[ ‘d’ ])
import pandas as pd
data = { ‘first’ : pd.Series( [ 1,2,3,4,5 ], index= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] ),
‘second’ : pd.Series( [ 10,20,30,40,50 ], index= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] ) }
df=pd.DataFrame(data)
print (df.iloc[ 2 ])
import pandas as pd
data = { ‘first’ : pd.Series( [ 1,2,3,4,5 ], index= [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] ),
‘second’ : pd.Series( [ 10,20,30,40,50 ], index= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ] ) }
df=pd.DataFrame(data)
print (df [ 3 : 5 ])