Python基础教程(四)Pandas–DataFrame

DataFrame数据框架是一种二维数据结构,即数据按行和列的表格方式排列。

DataFrame的创建

可以使用以下构造函数创建一个pandas DataFrame –

pandas.DataFrame( data, index, columns, dtype, copy)

No参数说明1data数据采用各种形式,如ndarray,序列,地图,列表,字典,常量和另一个DataFrame。2index对于行标签,如果没有索引被传递,则要用于结果帧的索引是可选缺省值np.arrange(n)。3columns对于列标签,可选的默认语法是 – np.arrange(n)。这只有在没有通过索引的情况下才是正确的。4dtype每列的数据类型。5copy如果默认值为False,则使用该命令(或其它)复制数据。

DataFrame可以使用单个列表或列表列表创建。
例1

data = [1,2,3,4]
df = pd.DataFrame(data,index=[list('abcd')],columns=['data'])
print(df)
   data
a     1
b     2
c     3
d     4
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print(data)
print( df)
[['Alex', 10], ['Bob', 12], ['Clarke', 13]]
     Name  Age
0    Alex   10
1     Bob   12
2  Clarke   13

字典列表可以作为输入数据传递以创建DataFrame。字典键默认作为列名。

df1 = pd.DataFrame({'one':[1,2,3],'two':[4,5,6]},index=['a','b','c'])
print(df1)
   one  two
a    1    4
b    2    5
c    3    6

DataFrame的常用属性

No属性说明1index获取索引2T转置3columns获取列索引4values获取值数组5describe()获取快速统计

例子:

df1 = pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),\
                    'two':pd.Series([4,5,6,7],index=['a','b','c','d'])},index=['a','b','c','d'])
print(df1)
print(df1.index)
Index(['a', 'b', 'c', 'd'], dtype='object')
print(df1.columns)
Index(['one', 'two'], dtype='object')
print(df1.values)
[[ 1.  4.]
 [ 2.  5.]
 [ 3.  6.]
 [nan  7.]]
print(df1.T)
       a    b    c    d
one  1.0  2.0  3.0  NaN
two  4.0  5.0  6.0  7.0
print(df1.describe())
       one       two
count  3.0  4.000000
mean   2.0  5.500000
std    1.0  1.290994
min    1.0  4.000000
25%    1.5  4.750000
50%    2.0  5.500000
75%    2.5  6.250000
max    3.0  7.000000

索引和切片

与Series相似,建议使用loc方法选取。并且不要使用连续两个中括号。

  • loc属性:将索引解释为标签
  • 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 ['one']
a     1.0
b     2.0
c     3.0
d     NaN
Name: one, dtype: float64
  • 按标签选择:可以通过将行标签传递给 loc 函数来选择行。
  • 按整数位置选择:行可以通过将整数位置传递给 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.loc['b']
one 2.0
two 2.0
Name: b, dtype: float64
df1 = pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),\
                    'two':pd.Series([4,5,6,7],index=['a','b','c','d'])},index=['a','b','c','d'])
print(df1)
print(df1.loc['a','one'])
print(df1.loc['a',:])
   one  two
a  1.0    4
b  2.0    5
c  3.0    6
d  NaN    7
1.0
one    1.0
two    4.0
Name: a, dtype: float64

数据对齐和数据缺失问题


df1 = pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),\
                    'two':pd.Series([4,5,6,7],index=['a','b','c','d'])},index=['a','c','d','b'])
print(df1)
df2 = pd.DataFrame({'one':pd.Series([1,2,3,8],index=['a','b','c','d']),\
                    'two':pd.Series([4,5,6,7],index=['a','b','c','d'])},index=['a','c','d','b'])
print(df2)
print(df1+df2)
   one  two
a  1.0    4
c  3.0    6
d  NaN    7
b  2.0    5
   one  two
a    1    4
c    3    6
d    8    7
b    2    5
   one  two
a  2.0    8
c  6.0   12
d  NaN   14
b  4.0   10

方法描述isnull判断是否为缺失值notnull判断不是缺失值fillna填充缺失值dropna删除缺失值,含有axis 参数。默认情况下,axis = 0,即沿着行,这意味着如果行内的任何值为NA,则排除整行。

例子:

df3 = df1+df2
df3.loc['b','one'] = np.nan
df3.loc['b','two'] = np.nan
print(df3)
   one   two
a  2.0   8.0
c  6.0  12.0
d  NaN  14.0
b  NaN   NaN
print(df1.fillna(0))
   one   two
a  2.0   8.0
c  6.0  12.0
d  NaN  14.0
b  NaN   NaN

NaN所在的行列全部丢弃

print(df1.dropna())
print(df3.dropna(how='any'))
   one   two
a  2.0   8.0
c  6.0  12.0

所在列或者行全为NaN,就把该行或者列删除

print(df3.dropna(how='all'))
   one   two
a  2.0   8.0
c  6.0  12.0
d  NaN  14.0

Original: https://blog.csdn.net/ngany/article/details/113802958
Author: ngany
Title: Python基础教程(四)Pandas–DataFrame

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/754116/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球