SELECT, ADD, AND DELETE COLUMNS IN DataFrames
This section contains short code blocks that illustrate how to perform operations on a DataFrame that resemble the operations on a Python dictionary. For example, getting, setting, and deleting columns works with the same syntax as the analogous Python dict operations, as shown here:
df = pd.DataFrame.from_dict(dict([('A',[1,2,3]),('B',[4,5,6])]),
orient='index', columns=['one', 'two', 'three'])
print(df)
The output from the preceding code snippet is here:
one two three A 1 2 3 B 4 5 6
Now look at the following operation that appends a new column to the contents of the DataFrame df:
df['four'] = df['one'] * df['two'] print(df)
The output from the preceding code block is here:
one two three four A 1 2 3 2 B 4 5 6 20
The following operation squares the contents of a column...