Example:
import
pandas as
pd
d =
{
'one'
:
pd.
Series
([
1
,
2
,
3
],
index=[
'a'
,
'b'
,
'c'
]),
'two'
:
pd.
Series
([
1
,
2
,
3
,
4
],
index=[
'a'
,
'b'
,
'c'
,
'd'
])}
df =
pd.
DataFrame
(
d)
print
df [
'one'
]
Its output
is as follows −
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
Column Addition:
We will understand this by adding a new column to an existing data frame.
Example:
import
pandas as
pd
d =
{
'one'
:
pd.
Series
([
1
,
2
,
3
],
index=[
'a'
,
'b'
,
'c'
]),
'two'
:
pd.
Series
([
1
,
2
,
3
,
4
],
index=[
'a'
,
'b'
,
'c'
,
'd'
])}
df =
pd.
DataFrame
(
d)
# Adding a new column to an existing DataFrame object with column label by passing new series
print
(
"Adding a new column by passing as Series:"
)
df[
'three'
]=
pd.
Series
([
10
,
20
,
30
],
index=[
'a'
,
'b'
,
'c'
])
print
df
print
(
"Adding a new column using the existing columns in DataFrame:"
)
df[
'four'
]=
df[
'one'
]+
df[
'three'
]
print
df
Its output
is as follows −
Adding a new column by passing as Series:
one two three
a 1.0 1 10.0
b 2.0 2 20.0
c 3.0 3 30.0
d NaN 4 NaN
Adding a new column using the existing columns in DataFrame:
one two three four
a 1.0 1 10.0 11.0
b 2.0 2 20.0 22.0
c 3.0 3 30.0 33.0
d NaN 4 NaN NaN
Column Deletion:
Columns can be deleted or popped; let us take an example to understand how.
Example:
# Using the previous DataFrame, we will delete a column
# using del function
import
pandas as
pd
d =
{
'one'
:
pd.
Series
([
1
,
2
,
3
],
index=[
'a'
,
'b'
,
'c'
]),
'two'
:
pd.
Series
([
1
,
2
,
3
,
4
],
index=[
'a'
,
'b'
,
'c'
,
'd'
]),
'three'
:
pd.
Series
([
10
,
20
,
30
],
index=[
'a'
,
'b'
,
'c'
])}
df =
pd.
DataFrame
(
d)
print
(
"Our dataframe is:"
)
print
df
# using del function
print
(
"Deleting the first column using DEL function:"
)
del
df[
'one'
]
print
df
# using pop function
print
(
"Deleting another column using POP function:"
)
df.
pop(
'two'
)
print
df
Its output
is as follows −
Our dataframe is:
one three two
a 1.0 10.0 1
b 2.0 20.0 2
c 3.0 30.0 3
d NaN NaN 4
Deleting the first column using DEL function:
three two
a 10.0 1
b 20.0 2
c 30.0 3
d NaN 4
Deleting another column using POP function:
three
a 10.0
c 30.0
d NaN
Row Selection, Addition, and Deletion:
We will now understand row selection, addition and deletion through examples. Let us begin with the concept of selection.
Selection by Label:
Rows can be selected by passing row label to a loc
function.