Working with data frame in pandas python involve manipulation of rows and columns of data frame by different methods which are described  below

Working with Data Frame in Pandas Python:

First we create a dataframe as shown below which has 3 columns and 3 rows.For details what is data frame you can click on series and dataframe in python

1.How to create Data Frame:

df = pd.DataFrame(
{
"Name": [
"Harris",
" William",
"Bonnell"]
,
"Age": [18, 20, 40],
"Sex": ["male", "female", "female"],
}
)
Output:
Name	Age	Sex
0	Harris	18	male
1	William	20	female
2	Bonnell	40	female

2.Filtering & Selecting in Data Frame:

Shape of data frame can be checked by typing df.shape as shown below

df.shape
Output:
(3,3)
#3 rows and 3 columns

3.For selecting name and age column only:

df[['Name','Age']]
Output:
Name	Age
0	Harris	18
1	William	20
2	Bonnell	40

4. For selecting rows only by name and age:

df[(df['Name']==' William')]
Output=  Name	Age	Sex
1	William	20	female
# age is greater than 30
df[df['Age']>30]
Output=  Name	Age	Sex
2	Bonnell	40	female

5.Select rows and columns by index using iloc:

df.iloc(rowindex,colindex)
df.iloc[1:3,0:2]
#it will output row 1:3(2nd and 3rd index),Columns(1st and 2nd index)
Output:
Name	Age
1	William	20
2	Bonnell	40

6. Select rows and columns with any criteria using loc:

# if you want to  find names of the person who has age greater than 30
df.loc[df['Age']>30,'Name']
Output:
2    Bonnell

Above  methods are used for working with data frame in pandas python.

By SC

Leave a Reply

Your email address will not be published. Required fields are marked *