Python Learning Note (iv): Pandas basics

Source: Internet
Author: User

Pandas Foundation Serise
importas pdfromimport= Series([4-753])obj
0    41   -72    53    3dtype: int64
obj.values
array([ 4, -7,  5,  3], dtype=int64)
obj.index
RangeIndex(start=0, stop=4, step=1)
obj[[1,3]]# 跳着选取数据
1   -73    3dtype: int64
obj[1:3]
1   -72    5dtype: int64
pd.isnull(obj)
0    False1    False2    False3    Falsedtype: bool
    • Reindex can be used to interpolate values
obj.reindex(range(5='ffill')
0    41   -72    53    34    3dtype: int64
    • Tag slices are closed intervals.
Dataframe
= {'state': ['asd','qwe','sdf','ert'],       'year': [2000200120022003],       'pop': [1.5,1.7,3.6,2.4= DataFrame(data)data
State Year
Pop
0 1.5 Asd 2000
1 1.7 Qwe 2001
2 3.6 Sdf 2002
3 2.4 Ert 2003
data.year# 比r里提取列要方便点
0    20001    20012    20023    2003Name: year, dtype: int64
data['debt'=range(4)data
State Year
Pop Debt
0 1.5 Asd 2000 0
1 1.7 Qwe 2001 1
2 3.6 Sdf 2002 2
3 2.4 Ert 2003 3
    • Index is not modifiable.
= data.indexa[1=6
---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-9-57677294f950> in <module>()      1 a = data.index----> 2 a[1] = 6F:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)   1668    1669     def __setitem__(self, key, value):-> 1670         raise TypeError("Index does not support mutable operations")   1671    1672     def __getitem__(self, key):TypeError: Index does not support mutable operations
data.columns
Index(['pop', 'state', 'year', 'debt'], dtype='object')
    • . IX label indexing function, entering rows and columns
    • No. IX can only select one of the columns or a row, not the column and row selection
data[:3]
State Year
Pop Debt
0 1.5 Asd 2000 0
1 1.7 Qwe 2001 1
2 3.6 Sdf 2002 2
data.ix[:,:3]
State Year
Pop
0 1.5 Asd 2000
1 1.7 Qwe 2001
2 3.6 Sdf 2002
3 2.4 Ert 2003
    • Delete a column with Drop,axis = 0 for the row, and 1 for the column
    • The original data is unchanged after deletion
data.drop(0,axis=0)
State Year
Pop Debt
1 1.7 Qwe 2001 1
2 3.6 Sdf 2002 2
3 2.4 Ert 2003 3
data.drop('year', axis=1)
State
Pop Debt
0 1.5 Asd 0
1 1.7 Qwe 1
2 3.6 Sdf 2
3 2.4 Ert 3
data
State Year
Pop Debt
0 1.5 Asd 2000 0
1 1.7 Qwe 2001 1
2 3.6 Sdf 2002 2
3 2.4 Ert 2003 3
importas= DataFrame(np.arange(9).reshape(33))df
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
    • Applymap () can apply a function to each element of Dataframe
    • Apply () can apply a function to each dimension array
df.applymap(lambda'%.2f'% x)
0 1 2
0 0.00 1.00 2.00
1 3.00 4.00 5.00
2 6.00 7.00 8.00
data.sort_values(by='pop')# 对某一列排序
State Year
Pop Debt
0 1.5 Asd 2000 0
1 1.7 Qwe 2001 1
3 2.4 Ert 2003 3
2 3.6 Sdf 2002 2
data.describe()
Year
Pop Debt
Count 4.000000 4.000000 4.000000
Mean 2.300000 2001.500000 1.500000
Std 0.948683 1.290994 1.290994
Min 1.500000 2000.000000 0.000000
25% 1.650000 2000.750000 0.750000
50% 2.050000 2001.500000 1.500000
75% 2.700000 2002.250000 2.250000
Max 3.600000 2003.000000 3.000000
df.isin([1])
0 1 2
0 False True False
1 False False False
2 False False False
    • None, Nan will be treated as NA
    • Df.shape without parentheses is equivalent to dim ()
df.shape
(3, 3)
    • Dropna Delete missing values
df.ix[:1, :1=Nonedf
0 1 2
0 NaN NaN 2
1 NaN NaN 5
2 6.0 7.0 8
    • Fill missing values can call dictionary, do not add different values
df.fillna({0:111:22})
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
df
0 1 2
0 NaN NaN 2
1 NaN NaN 5
2 6.0 7.0 8
df.fillna({0:111:22}, inplace=True)
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
df
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
    • InPlace modifying objects does not produce a copy

Python Learning Note (iv): Pandas basics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.