Pandas DataFrame 读取 添加和删除

Pandas DataFrame 读取 添加和删除上文介绍了如何创建 DataFrame,本章介绍如何对 Pandas.DataFrame 进行读取,添加和删除。

Pandas DataFrame 读取 添加和删除

DataFrame 读取列

下面将从数据帧(DataFrame)中读取一列。

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'])

执行结果如下:

a    1.0
b    2.0
c    3.0
d    NaN
Name: one, dtype: float64

DataFrame 添加列

下面演示如何向一个 DataFrame 中添加一个新列

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)

执行结果如下:

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

DataFrame 删除列

下面演示 DataFrame 中如何删除或弹出一列

# 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)

执行结果如下:

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
b   20.0
c   30.0
d    NaN

DataFrame 读取行

按索引选择
可以通过将行索引传递给loc()函数来选择行

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.loc['b'])

执行结果如下:

one    2.0
two    2.0
Name: b, dtype: float64

按位置选择
可以通过将整数位置传递给iloc()函数来选择行

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.iloc[2])

执行结果如下:

one    3.0
two    3.0
Name: c, dtype: float64

按行切片选择
可以使用:运算符选择多行

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[2:4])

执行结果如下:

   one  two
c  3.0    3
d  NaN    4

DataFrame 添加行

使用append()函数将新行添加到 DataFrame

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)
print (df)

执行结果如下:

   a  b
0  1  2
1  3  4
0  5  6
1  7  8

DataFrame 删除行

使用索引标签从 DataFrame 中删除行。 如果标签重复,则会删除多行。

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)

# Drop rows with label 0
df = df.drop(0)

print (df)

执行结果如下:

   a  b
1  3  4
1  7  8

在上面的例子中,一共有两行被删除,因为这两行包含相同的标签0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程