apply and applymap

1. Can be used directly NumPy Function of

Sample code :

# Numpy ufunc function 
df = pd.DataFrame(np.random.randn(5,4) - 1)
print(df) print(np.abs(df))

Running results :

 0 1 2 3
0 -0.062413 0.844813 -1.853721 -1.980717
1 -0.539628 -1.975173 -0.856597 -2.612406
2 -1.277081 -1.088457 -0.152189 0.530325
3 -1.356578 -1.996441 0.368822 -2.211478
4 -0.562777 0.518648 -2.007223 0.059411 0 1 2 3
0 0.062413 0.844813 1.853721 1.980717
1 0.539628 1.975173 0.856597 2.612406
2 1.277081 1.088457 0.152189 0.530325
3 1.356578 1.996441 0.368822 2.211478
4 0.562777 0.518648 2.007223 0.059411

2. adopt apply Apply a function to a column or row

Sample code :

# Use apply Apply row or column data 
#f = lambda x : x.max()
print(df.apply(lambda x : x.max()))

Running results :

0 -0.062413
1 0.844813
2 0.368822
3 0.530325
dtype: float64

Note the direction of the specified axis , Default axis=0, The direction is the column

Sample code :

# Specify the axis direction ,axis=1, The direction is OK 
print(df.apply(lambda x : x.max(), axis=1))

Running results :

0 0.844813
1 -0.539628
2 0.530325
3 0.368822
4 0.518648
dtype: float64

3. adopt applymap Apply functions to each data

Sample code :

# Use applymap Apply to every data 
f2 = lambda x : '%.2f' % x
print(df.applymap(f2))

Running results :

 0 1 2 3
0 -0.06 0.84 -1.85 -1.98
1 -0.54 -1.98 -0.86 -2.61
2 -1.28 -1.09 -0.15 0.53
3 -1.36 -2.00 0.37 -2.21
4 -0.56 0.52 -2.01 0.06

Sort

1. Index sort

sort_index()

Sort by default uses ascending sort ,ascending=False Sort in descending order

Sample code :

# Series
s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5))
print(s4) # Index sort
s4.sort_index() # 0 0 1 3 3

Running results :

0 10
3 11
1 12
3 13
0 14
dtype: int64 0 10
0 14
1 12
3 11
3 13
dtype: int64

Yes DataFrame Pay attention to the axis direction during operation

Sample code :

# DataFrame
df4 = pd.DataFrame(np.random.randn(3, 5),
index=np.random.randint(3, size=3),
columns=np.random.randint(5, size=5))
print(df4) df4_isort = df4.sort_index(axis=1, ascending=False)
print(df4_isort) # 4 2 1 1 0

Running results :

 1 4 0 1 2
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
1 1.988321 -0.466987 2.787891 -1.105912 0.889082 4 2 1 1 0
2 -0.161256 1.164138 -0.416686 -0.004294 0.088802
1 0.531256 -0.342573 -0.671914 -0.509493 0.303222
1 -0.466987 0.889082 1.988321 -1.105912 2.787891

2. Sort by value

sort_values(by='column name')

Sort by a unique column name , If there are other identical column names, an error will be reported .

Sample code :

# Sort by value 
df4_vsort = df4.sort_values(by=0, ascending=False)
print(df4_vsort)

Running results :

 1 4 0 1 2
1 1.988321 -0.466987 2.787891 -1.105912 0.889082
1 -0.671914 0.531256 0.303222 -0.509493 -0.342573
2 -0.416686 -0.161256 0.088802 -0.004294 1.164138

Processing missing data

Sample code :

df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],
[np.nan, 4., np.nan], [1., 2., 3.]])
print(df_data.head())

Running results :

 0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 NaN
2 NaN 4.000000 NaN
3 1.000000 2.000000 3.000000

1. Determine if there are missing values :isnull()

Sample code :

# isnull
print(df_data.isnull())

Running results :

 0 1 2
0 False False False
1 False False True
2 True False True
3 False False False

2. Discard missing data :dropna()

according to axis Axis direction , Discard contains NaN Row or column of . Sample code :

# dropna
print(df_data.dropna()) print(df_data.dropna(axis=1))

Running results :

 0 1 2
0 -0.281885 -0.786572 0.487126
3 1.000000 2.000000 3.000000 1
0 -0.786572
1 2.000000
2 4.000000
3 2.000000

3. Fill in missing data :fillna()

Sample code :

# fillna
print(df_data.fillna(-100.))

Running results :

 0 1 2
0 -0.281885 -0.786572 0.487126
1 1.000000 2.000000 -100.000000
2 -100.000000 4.000000 -100.000000
3 1.000000 2.000000 3.000000

Python Introduction to data analysis ( 6、 ... and ):Pandas Function application of more related articles

  1. Python Introduction to data analysis pandas Basic summary

    Pandas--" panda " Basics Series Series: pandas My long gun ( A column or row in a data table , The observation vector , One dimensional array ...) Series1 = pd.Series(np.r ...

  2. Python Introduction to data analysis

    Python Introduction to data analysis lately ,Analysis with Programming Joined the Planet Python. As the first special blog of the website , I'd like to share with you how to pass Python To start data analysis . Specifically ...

  3. Python Introduction and practice of data analysis

    Python Introduction and practice of data analysis ( Learning alone may be boring , But look for more like-minded friends together , Learning will become more meaningful ) It's a data-driven era , Want to engage in machine learning . Artificial intelligence . Data mining and other cutting-edge technologies , Cannot leave ...

  4. Python Data analysis tools :Pandas And Series

    Python Data analysis tools :Pandas And Series Pandas summary Pandas yes Python A data analysis package of , The tool is created to solve data analysis tasks .Pandas Include large databases and standard data models , Provides efficient operands ...

  5. Python Reptile introduction 6 Cookie Use

    Hello, everyone , In the last section, we looked at exception handling for crawlers , So let's take a look at Cookie Use . Why use Cookie Well ? Cookie, Refers to some websites in order to identify users . Conduct session Track and store in ...

  6. Python Introduction and practice of data analysis

    Python Introduction and practice of data analysis The whole course is over , The sharing of this course can be seen below , There is a link below , Before java Development has also been done for some years , Also share the feeling of watching this video , It's no problem to talk about single knowledge course itself , You can turn it off when you watch it ...

  7. python Introduction to data analysis ( One )---- install pandas

    I'm going to go into the pit , python Data analysis , So I downloaded < utilize python Data analysis > E-books , Photocopies , 14 Published in 2002 , There are a lot of tools that don't match right now , But the whole idea remains the same , So the preparation should be ...

  8. python Introduction to data analysis learning notes

    Learn to use python Notes for data analysis & What's going to be talked about in the internal exchange meeting next Tuesday , Share with you . The blogger is careless , If there is anything wrong, please correct me ~ There are still many things to be improved , When I study and improve ~ Preface : All kinds of data are divided into ...

  9. python Introduction to data analysis learning notes

    Learn to use python Notes for data analysis & What's going to be talked about in the internal exchange meeting next Tuesday , Share with you . The blogger is careless , If there is anything wrong, please correct me ~ There are still many things to be improved , When I study and improve ~ Preface : All kinds of information and data ...

  10. Python Introduction to data analysis

    Reprinted from https://blog.csdn.net/lijinlon/article/details/81517699 Data analysis by Python introduction 1. Duplicate data processing stay Da ...

Random recommendation

  1. Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

    Today I met Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:] This mistake , There has been no Baidu , Unexpectedly, I got one ...

  2. PhotoKit Introduction and use of the framework

    PhotoKit It's a set of comparison AssetsLibrary Updating is more complete and efficient ios Photo processing library , The processing of resources is similar to AssetsLibrary There's a big difference . Here is a brief introduction PhotoKit Some basic concepts of P ...

  3. webstorage[html5 Local data processing of ]

    1.webStorage What is it? ? webStorage yes html5 A way to localize storage in , Before, we used to use cookie It's stored in the same way ; 2. So what's the difference between them ? Ⅰ.cookie The problem is : ...

  4. document.elementFromPoint stay IE8 The solution to the problem of unable to obtain the current coordinate element stably under the

    document.elementFromPoint(e.clientX, e.clientY) document.elementFromPoint(e.clientX, e.clientY) perform 2 Time ...

  5. Computers can't browse the web but Can log in qq, Solution summary

    The computer can log in qq, But you can't browse the web , Solution summary [ notes ]: For reference only , It's personal experience I got up this morning , All of a sudden, the computer can only log in qq, I can't get on the Internet , Never encountered this problem, I began a variety of Baidu methods , Various attempts , Finally, the problem was solved successfully The first ...

  6. sparkSQL1.1 introduction

    http://blog.csdn.net/book_mmicky/article/details/39288715 2014 year 9 month 11 Japan ,Spark1.1.0 All of a sudden . I download it immediately . compile . Deployed S ...

  7. 【win8 skill 】 How to search for apps in the app store app

    win8 App store search app Software skills 1. Composite key WIN+C Open the magnetic stripe on the far right of the screen 2. Click on the search , Enter the name of the software you want to search , It will list the ones you have installed app Or you can click on the app store option below , You can search the Internet app providers ...

  8. 【POJ1113】Wall( convex hull )

    [ subject ] Description Once upon a time there was a greedy King who ordered his chief Architect to build a ...

  9. CentOS、Ubuntu Configure the NIC sub interface

    CentOS # ip addr add dev eth0 lable eth0: These are temporary configurations , Restart failure . If you want to keep it forever , Add network profile # vim /etc/sysconfig/network- ...

  10. Spring boot Integrate redis Stand-alone version

        One . install redis I won't go into that , There are various systems installed on the Internet redis The operation of , redis install Two . establish sprigboot project I don't say much about this , No, there are related tutorials ahead . 3、 ... and . add to maven coordinate Four . To write spr ...