這篇文章主要介紹Pandas常用的功能有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
首先我們還是隨機產生一個數據表,5行3列的數據框。保存到csv文件并讀取。
import pandas as pd
import numpy as np
sample = np.array(np.random.randint(0,100, size=15))
sample_reshape = sample.reshape((5,3))
sample_pd = pd.DataFrame(sample_reshape)
sample_pd.to_csv("sample.csv",header=None, index=None)
import pandas as pd
import numpy as np
sample = pd.read_csv("sample.csv", header=None)
print sample.head()
"""
0 1 2
0 6 40 24
1 5 24 56
2 59 21 44
3 58 4 25
4 83 74 58
"""
?!∨判?/p>
首先介紹一下如何對數據框進行排序,總的來說,pandas提供兩種排序方法,一個是根據索引值排序,一個是根據數據框中某一列或者某一行排序,這個就和Excel中的排序是一樣的,但是它排序的結果是擴展到整個數據表的,不是按照單獨一行或者一列排序,如果要對行或者列單獨排序,可以首先把行或者列索引出來,然后在排序。
## sort_index
by參數指定列名,axis默認為0, 桉列排序,排序之后得到4, 21, 24,40, 74,可以指定axis為1,按行排序, 結果為5, 24, 56。
import pandas as pd
sample = pd.read_csv("sample.csv", header=None)
sort_index_1 = sample.sort_index(by=1)
print sort_index_1
"""
0 1 2
3 58 4 25
2 59 21 44
1 5 24 56
0 6 40 24
4 83 74 58
"""
sort_index_axis_1 = sample.sort_index(by=1, axis=1)
print sort_index_axis_1
"""
0 1 2
0 6 40 24
1 5 24 56
2 59 21 44
3 58 4 25
4 83 74 58
"""
ascending參數指定降序排序,由大到小。
sort_index_ascend = sample.sort_index(by=1, ascending=False)
print sort_index_ascend
"""
0 1 2
4 83 74 58
0 6 40 24
1 5 24 56
2 59 21 44
3 58 4 25
"""
##sort_values
通過結果,我們發現sort_values和sort_index幾乎是相同的。But, sort_index后面將會被棄用。。。所以大家也可以只學習sort_values的用法。
import pandas as pd
sample = pd.read_csv("sample.csv", header=None)
sample_sort_value = sample.sort_values(by=1)
print sample_sort_value
print " - * - " * 5
sample_sort_axis = sample.sort_values(by=1, axis=1)
print sample_sort_axis
print " - * - " * 5
sort_value_ascend = sample.sort_values(by=1, ascending=False)
print sort_value_ascend
"""
0 1 2
3 58 4 25
2 59 21 44
1 5 24 56
0 6 40 24
4 83 74 58
- * - - * - - * - - * - - * -
0 1 2
0 6 40 24
1 5 24 56
2 59 21 44
3 58 4 25
4 83 74 58
- * - - * - - * - - * - - * -
0 1 2
4 83 74 58
0 6 40 24
1 5 24 56
2 59 21 44
3 58 4 25
"""
下面我們看個稍微高級點的玩法,如果要按照某一行或者列的最大值來排序,該怎么做。首先我們新添加一列,用來求每一行的最大值。然后我們根據最大值降序排序就可以了。
import pandas as pd
sample = pd.read_csv("sample.csv", header=None)
sample['row_max'] = sample.apply(lambda x: x.max(), axis=1)
new = sample.sort_values(by='row_max', ascending=False)
print new
"""
0 1 2 row_max
4 83 74 58 83
2 59 21 44 59
3 58 4 25 58
1 5 24 56 56
0 6 40 24 40
"""
學會怎么按照最大值排序,那么按照其他統計量也就可以了,比如均值,最小值等等。
# apply, applymap, map
這三個函數中,前兩個是針對DataFrame使用的, 而map是針對Series使用的。 首先看一下函數文檔,也就基本清楚他們怎么用了。
DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)
DataFrame.applymap(func)
Series.map(arg, na_action=None)
apply函數是將一個函數func,應用到DataFrame的元素中,其中axis指定數據的維度,其他幾個參數不常用,這里不說了, 然后大家有需要用的時候可以去看看。applymap是將函數func直接應用到每一個元素中;map函數是將值和某個Series對應起來,下面看個栗子。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3, 3))
print df
print
df = df.applymap(lambda x: '%.2f' % x)
print df
"""
0 1 2
0 0.776506 -0.605382 1.843036
1 0.522743 1.267487 1.288286
2 0.495450 0.583332 -0.590918
0 1 2
0 0.78 -0.61 1.84
1 0.52 1.27 1.29
2 0.50 0.58 -0.59
"""
import pandas as pd
x = pd.Series([1, 2, 3], index=['one', 'two', 'three'])
print x
y = pd.Series(['foo', 'bar', 'baz'], index=[1, 2, 3])
print
print y
print
print x.map(y)
"""
one 1
two 2
three 3
dtype: int64
1 foo
2 bar
3 baz
dtype: object
one foo
two bar
three baz
dtype: object
"""
# 分組
DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, **kwargs)
然后還是之前的數據,我們新添加一列,列名為key1,分組的意思就是將數據框以某種標志分為不同的組,這里選擇key1作為分組依據,這樣就分為了兩組,分組的作用的我們可以分別統計各自組內的統計量。比如要分析不同性別,不同年齡段等等問題的時候,就會用到分組統計。
注意這里grouped是一個SeriesGroupBy 對象,具體統計的時候,需要用SeriesGroupBy 的方法。
import pandas as pd
sample = pd.read_csv("sample.csv", header=None)
sample['key1'] = ['a', 'b', 'b', 'a', 'b']
print sample
print
grouped = sample[1].groupby(sample['key1'])
print grouped
print
print grouped.mean()
print
print grouped.max()
"""
0 1 2 key1
0 6 40 24 a
1 5 24 56 b
2 59 21 44 b
3 58 4 25 a
4 83 74 58 b
<pandas.core.groupby.SeriesGroupBy object at 0x0000000005E2ED68>
key1
a 22.000000
b 39.666667
Name: 1, dtype: float64
key1
a 40
b 74
Name: 1, dtype: int64
"""
以上是“Pandas常用的功能有哪些”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。