03 - Introduction To Pandas 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

df

# drop returns the updated DataFrame

# often then case that we 'catch' this in another variable, leaving the original unchanged

x = df.drop('b')

# Can also drop multiple rows

x = df.drop(['b','c'])

# Can use drop to remove a column

# Use the axis parameter to specify we are referring to columns

x = df.drop('two', axis=1)

# or more descriptively

x = df.drop('two', axis='columns')

# Usually drop is for rows and pop is for columns

# pop WILL remove a column from the DataFrame

# pop returns the column removed

# often a good idea to catch the removed column in a new variable

x = df.pop('two')

df

# del deletes a column and returns nothing

# very unforgiving

del df['three']

df

Appending Columns
very easy to accomplish
E.g. To add a column populated with data to the end of your DataFrame
df['NAME OF COLUMN'] = data
if the column already exists, it will overwrite the values in the existing column

In [ ]: # Add a new colum of 5's

# This will append a new column to the DataFrame

df['five'] = 5

# Add another column which is column 'one' multiplied by column 'four'

df['six'] = df['one'] * df['four']

df

Inserting Columns
Insert a column into the Google DataFrame between the 'Close' column and the 'Volume' column.

Call this column 'High-Low Spread'

Populate it with the difference between 'High' and 'Low'.

You might also like