概览
pandas.DataFrame
创建DataFrame
列表
字典
系列(Series)
列选择
列添加
列删除 pop/del
行选择,添加和删除
标签选择 loc
按整数位置选择 iloc
行切片
附加行 append
删除行 drop
数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列
数据帧(DataFrame)的功能特点:
构造函数:
pandas.DataFrame(data, index, columns, dtype, copy)
编号 | 参数 | 描述 |
---|---|---|
1 | data | 数据采取各种形式,如:ndarray,series,map,lists,dict,constant和另一个DataFrame。 |
2 | index | 对于行标签,要用于结果帧的索引是可选缺省值np.arrange(n),如果没有传递索引值。 |
3 | columns | 对于列标签,可选的默认语法是 - np.arange(n)。这只有在没有索引传递的情况下才是这样。 |
4 | dtype | 每列的数据类型。 |
5 | copy | 如果默认值为False,则此命令(或任何它)用于复制数据。 |
Pandas数据帧(DataFrame)可以使用各种输入创建
import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df
res:
0
0 1
1 2
2 3
3 4
4 5
import pandas as pd
data = [{
'a': 1, 'b': 2},{
'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data, index=['first', 'second'])
print df
res:
a b c
0 1 2 NaN
1 5 10 20.0
import pandas as pd
data = {
'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print df
res:
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
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
res:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
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']
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
# 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
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']
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]
使用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
使用索引标签从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
作者:Johngo
配图:Pexels