Adding to dataframes#

Load the initial data.

import pandas as pd

small_url = 'https://gist.githubusercontent.com/van-alfen/bca77fd9f4c0fcf74308bc7cfa12a8a7/raw/030a8fea2ddfd1b8fd257d5a601b15a2942c78d7/small_data.csv'
small_df = pd.read_csv(small_url)
small_df.set_index('ticker', inplace=True)
small_df
price name assets_current liab_current
ticker
AAPL 137 Apple Inc 162.82 105.72
MSFT 229 Microsoft Corp 181.92 72.31
GOOG 1677 Alphabet Inc 152.58 45.22
AMZN 3535 Amazon.com Inc 96.33 87.81

Adding rows#

We can append observations (rows) to a dataframe by using the .concat() method.

new_obs = pd.DataFrame(
	columns=['ticker', 'price', 'name', 'assets_current', 'liab_current'],
	data=[
		['TSLA', 1256, 'Tesla Inc', 27.1, 19.71],
	],
)
new_obs.set_index('ticker', inplace=True)
new_obs
price name assets_current liab_current
ticker
TSLA 1256 Tesla Inc 27.1 19.71
medium_df = pd.concat([small_df, new_obs])
medium_df
price name assets_current liab_current
ticker
AAPL 137 Apple Inc 162.82 105.72
MSFT 229 Microsoft Corp 181.92 72.31
GOOG 1677 Alphabet Inc 152.58 45.22
AMZN 3535 Amazon.com Inc 96.33 87.81
TSLA 1256 Tesla Inc 27.10 19.71

Adding columns#

Create a new column named nwc that is equal to current assets minus current liabilities.

small_df['nwc'] = small_df['assets_current'] - small_df['liab_current']
small_df
price name assets_current liab_current nwc
ticker
AAPL 137 Apple Inc 162.82 105.72 57.10
MSFT 229 Microsoft Corp 181.92 72.31 109.61
GOOG 1677 Alphabet Inc 152.58 45.22 107.36
AMZN 3535 Amazon.com Inc 96.33 87.81 8.52

Add a new expensive column that is equal to ‘yes’ if the price is higher than $1,000 and ‘no’ otherwise.

In this case we can use a filter inside .loc to set values in a new column.

expensive_rows_filter = ( small_df['price'] >  1000 )
cheap_rows_filter     = ( small_df['price'] <= 1000 )

small_df.loc[ expensive_rows_filter, 'expensive' ] = 'yes'
small_df.loc[ cheap_rows_filter,     'expensive' ] = 'no'
small_df
price name assets_current liab_current nwc expensive
ticker
AAPL 137 Apple Inc 162.82 105.72 57.10 no
MSFT 229 Microsoft Corp 181.92 72.31 109.61 no
GOOG 1677 Alphabet Inc 152.58 45.22 107.36 yes
AMZN 3535 Amazon.com Inc 96.33 87.81 8.52 yes