Converting Data Types:

  • Pandas provides a number of methods for converting data types in DataFrames.
  • Keep in mind that you need not do all your data type conversions at once, when you first get your data.
    • First, to get a list of the data types in a DataFrame, we can use the .dtypes attribute.
python
1import pandas as pd
2
3df = pd.read_csv('./Data/tips.csv')
4print(df.dtypes)

Output:

1total_bill    float64
2tip           float64
3sex            object
4smoker         object
5day            object
6time           object
7size            int64
8dtype: object

Converting to String Objects:

  • To convert values into strings, we can use the .astype() method on the column (i.e., Series) we want to convert.
  • The .astype() method takes a single parameter, dtype, which will be the new data type the column will take on.
    • For example, to convert the total_bill column to a string (or object), we can use the following code:
python
6df['total_bill'] = df['total_bill'].astype('str')
7print(df.dtypes)

Output:

1total_bill     object
2tip           float64
3sex            object
4smoker         object
5day            object
6time           object
7size            int64
8dtype: object

Converting to Numeric Objects:

  • The .astype() method is generic and can be used to convert any column in a DataFrame to another dtype.
  • Recall that each column in a DataFrame is a Pandas Series object.
  • The example below shows how to change the type of a DataFrame column, but if you are working with a Series object, you can use the same .astype() method to convert the Series as well.
python
9# Convert it back to float
10df['total_bill'] = df['total_bill'].astype('float')
11print(df.dtypes)

Output:

1total_bill    float64
2tip           float64
3sex            object
4smoker         object
5day            object
6time           object
7size            int64
8dtype: object

Sorting:

  • In order to sort a DataFrame, we can use the .sort_values() method.
  • The .sort_values() method takes a few parameters:
    • by: the column to sort by.
    • ascending: a boolean value that determines whether to sort in ascending or descending order.
    • inplace: a boolean value that determines whether to sort in place or return a new DataFrame.
      • The following statement for example will create a new DataFrame, dfsort, and sort it by the total_bill column in descending order. Our original DataFrame variable, df, will remain unchanged.
python
1dfsort = df.sort_values(by='total_bill', ascending=False)
2print(dfsort.head())

Output:

1     total_bill    tip   sex smoker  day    time  size
2170       50.81  10.00  Male    Yes  Sat  Dinner     3
3212       48.33   9.00  Male     No  Sat  Dinner     4
459        48.27   6.73  Male     No  Sat  Dinner     4
5156       48.17   5.00  Male     No  Sun  Dinner     6
6182       45.35   3.50  Male    Yes  Sun  Dinner     3
  • If instead we want to sort the original DataFrame, df, in place, we can use the inplace=True parameter:
python
1df.sort_values(by='total_bill', ascending=True, inplace=True)
2print(df.head())

Output:

1     total_bill   tip     sex smoker   day    time  size
267         3.07  1.00  Female    Yes   Sat  Dinner     1
392         5.75  1.00  Female    Yes   Fri  Dinner     2
4111        7.25  1.00  Female     No   Sat  Dinner     1
5172        7.25  5.15    Male    Yes   Sun  Dinner     2
6149        7.51  2.00    Male     No  Thur   Lunch     2

Sorting by Multiple Columns:

  • Sorting can also be done based on multiple columns.
    • Example: Sort by size (Most to Least) and then by tip (Least to Most).
  • if we’d like to sort by size and then by tip, we can pass a list of column names to the by parameter:
python
1df.sort_values(by=['size', 'tip'], ascending=[False, True], inplace=True)
2print(df.head(10))

Output:

1     total_bill   tip     sex smoker   day    time  size
2125       29.80  4.20  Female     No  Thur   Lunch     6
3143       27.05  5.00  Female     No  Thur   Lunch     6
4156       48.17  5.00    Male     No   Sun  Dinner     6
5141       34.30  6.70    Male     No  Thur   Lunch     6
6187       30.46  2.00    Male    Yes   Sun  Dinner     5
7216       28.15  3.00    Male    Yes   Sat  Dinner     5
8142       41.19  5.00    Male     No  Thur   Lunch     5
9185       20.69  5.00    Male     No   Sun  Dinner     5
10155       29.85  5.14  Female     No   Sun  Dinner     5
11153       24.55  2.00    Male     No   Sun  Dinner     4