"Name" 'Harry' 'Rohan' 'Skillf' 'Subh' "Marks" "City" 'Rampur' 'Kolkata' 'Bareley' 'Antartica'

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

19/10/2020 Untitled - Jupyter Notebook

In [1]: 

import numpy as np
import pandas as pd

In [2]: 

dict1={"name":['harry','rohan','skillf','subh'],
"marks":[943,43,45,345],
"city":['rampur','kolkata','bareley','antartica']}

In [3]: 

df =pd.DataFrame(dict1)

In [4]: 

df

Out[4]:

name marks city

0 harry 943 rampur

1 rohan 43 kolkata

2 skillf 45 bareley

3 subh 345 antartica

In [5]: 

df.to_csv('friends')

In [6]: 

df

Out[6]:

name marks city

0 harry 943 rampur

1 rohan 43 kolkata

2 skillf 45 bareley

3 subh 345 antartica

In [7]: 

df.to_csv('friends_index_false.csv', index=False)

localhost:8889/notebooks/Untitled.ipynb 1/23
19/10/2020 Untitled - Jupyter Notebook

In [8]: 

df

Out[8]:

name marks city

0 harry 943 rampur

1 rohan 43 kolkata

2 skillf 45 bareley

3 subh 345 antartica

In [9]: 

df.tail(2)

Out[9]:

name marks city

2 skillf 45 bareley

3 subh 345 antartica

In [10]: 

df.head(2)

Out[10]:

name marks city

0 harry 943 rampur

1 rohan 43 kolkata

localhost:8889/notebooks/Untitled.ipynb 2/23
19/10/2020 Untitled - Jupyter Notebook

In [11]: 

df.describe()

Out[11]:

marks

count 4.000000

mean 344.000000

std 423.793975

min 43.000000

25% 44.500000

50% 195.000000

75% 494.500000

max 943.000000

In [12]: 

vivek=pd.read_csv('vivek.csv')

In [13]: 

vivek

Out[13]:

Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed:


train Speed city
0 0.1 0.1.1 0.1.1.1 0.1.1.1.1 0.1.1.1.1.1

0 0 0 0 0 0 0 15462 50 rampur

1 1 1 1 1 1 1 58565 43 kolkata

2 2 2 2 2 2 2 52695 445 bareley

3 3 3 3 3 3 3 454911 345 antartica

In [14]: 

vivek['Speed']

Out[14]:

0 50
1 43
2 445
3 345
Name: Speed, dtype: int64

localhost:8889/notebooks/Untitled.ipynb 3/23
19/10/2020 Untitled - Jupyter Notebook

In [15]: 

vivek['Speed'][0]=50

<ipython-input-15-d9680192b8b7>:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/


stable/user_guide/indexing.html#returning-a-view-versus-a-copy (https://pand
as.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-v
ersus-a-copy)
vivek['Speed'][0]=50

In [16]: 

vivek

Out[16]:

Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed:


train Speed city
0 0.1 0.1.1 0.1.1.1 0.1.1.1.1 0.1.1.1.1.1

0 0 0 0 0 0 0 15462 50 rampur

1 1 1 1 1 1 1 58565 43 kolkata

2 2 2 2 2 2 2 52695 445 bareley

3 3 3 3 3 3 3 454911 345 antartica

In [17]: 

vivek.to_csv('vivek.csv')

In [18]: 

vivek.index =['first','second','third','fourth']

localhost:8889/notebooks/Untitled.ipynb 4/23
19/10/2020 Untitled - Jupyter Notebook

In [19]: 

vivek

Out[19]:

Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed:


train Speed
0 0.1 0.1.1 0.1.1.1 0.1.1.1.1 0.1.1.1.1.1

first 0 0 0 0 0 0 15462 50

second 1 1 1 1 1 1 58565 43

third 2 2 2 2 2 2 52695 445 b

fourth 3 3 3 3 3 3 454911 345 an

In [20]: 

ser=pd.Series(np.random.rand(34))

In [21]: 

ser.head()

Out[21]:

0 0.898952
1 0.862269
2 0.833553
3 0.062381
4 0.436343
dtype: float64

In [22]: 

type(ser)

Out[22]:

pandas.core.series.Series

In [23]: 

newdf=pd.DataFrame(np.random.rand(34))

localhost:8889/notebooks/Untitled.ipynb 5/23
19/10/2020 Untitled - Jupyter Notebook

In [24]: 

newdf.head()

Out[24]:

0 0.168435

1 0.342793

2 0.913369

3 0.303032

4 0.174320

In [25]: 

newwdf=pd.DataFrame(np.random.rand(34,5), index=np.arange(34))

In [26]: 

newwdf.head()

Out[26]:

0 1 2 3 4

0 0.108198 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [27]: 

type(newdf)

Out[27]:

pandas.core.frame.DataFrame

localhost:8889/notebooks/Untitled.ipynb 6/23
19/10/2020 Untitled - Jupyter Notebook

In [28]: 

newwdf.describe()

Out[28]:

0 1 2 3 4

count 34.000000 34.000000 34.000000 34.000000 34.000000

mean 0.547016 0.435715 0.584629 0.434930 0.418085

std 0.293346 0.290972 0.268924 0.306786 0.286042

min 0.055370 0.061017 0.013567 0.000584 0.003376

25% 0.305466 0.176032 0.383657 0.216141 0.243856

50% 0.623625 0.355529 0.664843 0.337707 0.383016

75% 0.769642 0.689130 0.786849 0.685055 0.483635

max 0.974352 0.991187 0.978071 0.989598 0.993456

In [29]: 

newwdf.dtypes

Out[29]:

0 float64
1 float64
2 float64
3 float64
4 float64
dtype: object

In [30]: 

newwdf[0][0]='vivek'

In [31]: 

newwdf.dtypes

Out[31]:

0 object
1 float64
2 float64
3 float64
4 float64
dtype: object

localhost:8889/notebooks/Untitled.ipynb 7/23
19/10/2020 Untitled - Jupyter Notebook

In [32]: 

newwdf.head()

Out[32]:

0 1 2 3 4

0 vivek 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [33]: 

newwdf.index

Out[33]:

Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33],
dtype='int64')

In [34]: 

newwdf.columns

Out[34]:

RangeIndex(start=0, stop=5, step=1)

localhost:8889/notebooks/Untitled.ipynb 8/23
19/10/2020 Untitled - Jupyter Notebook

In [35]: 

reform=newwdf.to_numpy()
print(reform.shape)
reform

(34, 5)

Out[35]:

array([['vivek', 0.16980631183741834, 0.801854064772398,


0.2647001075149201, 0.2406102893950759],
[0.8816274717495451, 0.17854900857159062, 0.6113007628409922,
0.3188941233932744, 0.3823913701642304],
[0.3473751395652329, 0.13487952631484645, 0.9780712128021622,
0.003456049682193152, 0.04282838190456417],
[0.6871006087593905, 0.15361385598090593, 0.6677226247622006,
0.3473210079746871, 0.3836406512270397],
[0.7347480266585339, 0.36478716237957554, 0.3101992711302316,
0.024578279030104122, 0.45676006099568855],
[0.14833095507929728, 0.14709531126504083, 0.8113274805336125,
0.4479466953001914, 0.40735785075607434],
[0.4988660742759661, 0.061016591166244916, 0.16687134854312624,
0.4918139498564672, 0.3772442747421516],
[0.9679045402718143, 0.3245144460957474, 0.7163223439066408,
0.550624171932092, 0.18726135064410276],
[0.5820521530030948, 0.6450344704937194, 0.6726303223127569,
0.1569430258538146, 0.07513283248248381],
[0.4308378748975886, 0.5976781468182369, 0.9545857218691216,
0.2312314593179564, 0.08102196787319993],
[0.7886648631998393, 0.08373009764752348, 0.870881621078923,
0.0005835723035602092, 0.48277342925909894],
[0.9248108984434181, 0.5219786852713755, 0.7388105936824615,
0.8604292206337808, 0.8454654543167198],
[0.6651988277712388, 0.2367417708514702, 0.6619629236459719,
0.774331620706889, 0.003376382119049892],
[0.07219363425519765, 0.4064986813252951, 0.7088557159650234,
0.03141010888488971, 0.9109807728839375],
[0.291495675599622, 0.7038283449425723, 0.9321138799497026,
0.9266825085279031, 0.33638166864738495],
[0.9242961777500177, 0.7837722477050866, 0.16703853514157785,
0.1513210173331978, 0.47326362933825283],
[0.7701808356996874, 0.9555959127290554, 0.525523611549857,
0.3280937128552105, 0.4344125647537831],
[0.24394071521876182, 0.821918130768973, 0.394067357821422,
0.9440397414078243, 0.42813786083417527],
[0.7095891431298501, 0.15500935088797418, 0.21267344297568647,
0.5820795532374756, 0.8544459781447977],
[0.05537045553374398, 0.20900332553830325, 0.43274856280576,
0.9895977737557116, 0.993456081753459],
[0.17477814311206963, 0.5102065021436899, 0.6034708116519792,
0.2331244971965365, 0.9107523358536886],
[0.7802155081690363, 0.8837077461860834, 0.8524703870338646,
0.8938698456700211, 0.4839228201695378],
[0.9743515197979845, 0.1182332206823109, 0.380187105920059,
0.3154922370015417, 0.2743645135523691],
[0.9137779612027577, 0.7920901451738167, 0.684754350742153,
0.8049105603762349, 0.6700796053874066],
[0.6702286822216333, 0.7922143605134271, 0.7247898974809844,
0.2111104623276815, 0.9130777647822043],
localhost:8889/notebooks/Untitled.ipynb 9/23
19/10/2020 Untitled - Jupyter Notebook

[0.21636796319809892, 0.34627115724141744, 0.8991080607249173,


0.5524916066274006, 0.47765816068027955],
[0.46719920810309634, 0.38581245875248427, 0.5854999590961536,
0.02094767420637378, 0.2939946461196098],
[0.0704983537473618, 0.9911870020242943, 0.01356665373364574,
0.16183871403201755, 0.09361438353635987],
[0.35164204711192326, 0.9018811659788789, 0.07114452957292539,
0.5887263039745945, 0.7232934067902513],
[0.5544229135080362, 0.3218055902792951, 0.24237958441483354,
0.29576031869755437, 0.04365505015686799],
[0.7680252837876755, 0.1751930656777586, 0.8381376385255432,
0.5417298770152795, 0.3063030290947599],
[0.6678533784785692, 0.28031749740766443, 0.7418338616250831,
0.7171644987220995, 0.07763206302890358],
[0.7480486402767317, 0.19474581500872612, 0.360343220209265,
0.7742351429271265, 0.2535944744005383],
[0.40835353061459934, 0.46558158298095054, 0.5441225933311339,
0.2501364331297705, 0.29600935768771297]], dtype=object)

In [36]: 

newwdf.T

Out[36]:

0 1 2 3 4 5 6 7

0 vivek 0.881627 0.347375 0.687101 0.734748 0.148331 0.498866 0.967905 0.5820

1 0.169806 0.178549 0.13488 0.153614 0.364787 0.147095 0.0610166 0.324514 0.6450

2 0.801854 0.611301 0.978071 0.667723 0.310199 0.811327 0.166871 0.716322 0.672

3 0.2647 0.318894 0.00345605 0.347321 0.0245783 0.447947 0.491814 0.550624 0.1569

4 0.24061 0.382391 0.0428284 0.383641 0.45676 0.407358 0.377244 0.187261 0.07513

5 rows × 34 columns

In [37]: 

newwdf.head()

Out[37]:

0 1 2 3 4

0 vivek 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

localhost:8889/notebooks/Untitled.ipynb 10/23
19/10/2020 Untitled - Jupyter Notebook

In [38]: 

sorted_newdf1=newwdf.sort_index(axis=0,ascending=False)
sorted_newdf1.head()

Out[38]:

0 1 2 3 4

33 0.408354 0.465582 0.544123 0.250136 0.296009

32 0.748049 0.194746 0.360343 0.774235 0.253594

31 0.667853 0.280317 0.741834 0.717164 0.077632

30 0.768025 0.175193 0.838138 0.541730 0.306303

29 0.554423 0.321806 0.242380 0.295760 0.043655

In [39]: 

sorted_newdf2=newwdf.sort_index(axis=1,ascending=False)
sorted_newdf2.head()

Out[39]:

4 3 2 1 0

0 0.240610 0.264700 0.801854 0.169806 vivek

1 0.382391 0.318894 0.611301 0.178549 0.881627

2 0.042828 0.003456 0.978071 0.134880 0.347375

3 0.383641 0.347321 0.667723 0.153614 0.687101

4 0.456760 0.024578 0.310199 0.364787 0.734748

In [40]: 

newwdf.head()

Out[40]:

0 1 2 3 4

0 vivek 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

localhost:8889/notebooks/Untitled.ipynb 11/23
19/10/2020 Untitled - Jupyter Notebook

In [41]: 

newdf2=newwdf

In [42]: 

newdf2[0][0]=22332

<ipython-input-42-b6b88ecefaab>:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/


stable/user_guide/indexing.html#returning-a-view-versus-a-copy (https://pand
as.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-v
ersus-a-copy)
newdf2[0][0]=22332

In [43]: 

newwdf.head()

Out[43]:

0 1 2 3 4

0 22332 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [44]: 

newdf2=newwdf.copy()

In [45]: 

newdf2[0][0]=54625659

<ipython-input-45-5a7394a84071>:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/


stable/user_guide/indexing.html#returning-a-view-versus-a-copy (https://pand
as.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-v
ersus-a-copy)
newdf2[0][0]=54625659

localhost:8889/notebooks/Untitled.ipynb 12/23
19/10/2020 Untitled - Jupyter Notebook

In [46]: 

newwdf.head()

Out[46]:

0 1 2 3 4

0 22332 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [47]: 

newwdf.loc[0,0]=1231

In [48]: 

newwdf.head(2)

Out[48]:

0 1 2 3 4

0 1231 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

In [49]: 

newwdf.columns=list("ABCDE")

In [50]: 

newwdf.head()

Out[50]:

A B C D E

0 1231 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

localhost:8889/notebooks/Untitled.ipynb 13/23
19/10/2020 Untitled - Jupyter Notebook

In [51]: 

newwdf.loc[0,'A']=99

In [52]: 

newwdf.head()

Out[52]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [53]: 

newdrop=newwdf.drop('A', axis=1)
newdrop.head()

Out[53]:

B C D E

0 0.169806 0.801854 0.264700 0.240610

1 0.178549 0.611301 0.318894 0.382391

2 0.134880 0.978071 0.003456 0.042828

3 0.153614 0.667723 0.347321 0.383641

4 0.364787 0.310199 0.024578 0.456760

In [54]: 

newwdf.head()

Out[54]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

localhost:8889/notebooks/Untitled.ipynb 14/23
19/10/2020 Untitled - Jupyter Notebook

In [55]: 

newwdf.head()

Out[55]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [56]: 

newwdf.loc[[1,2],:]

Out[56]:

A B C D E

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

In [57]: 

newwdf.loc[(newwdf['A']<0.3)]

Out[57]:

A B C D E

5 0.148331 0.147095 0.811327 0.447947 0.407358

13 0.0721936 0.406499 0.708856 0.031410 0.910981

14 0.291496 0.703828 0.932114 0.926683 0.336382

17 0.243941 0.821918 0.394067 0.944040 0.428138

19 0.0553705 0.209003 0.432749 0.989598 0.993456

20 0.174778 0.510207 0.603471 0.233124 0.910752

25 0.216368 0.346271 0.899108 0.552492 0.477658

27 0.0704984 0.991187 0.013567 0.161839 0.093614

localhost:8889/notebooks/Untitled.ipynb 15/23
19/10/2020 Untitled - Jupyter Notebook

In [58]: 

newwdf.head()

Out[58]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [59]: 

newwdf.iloc[0,4]

Out[59]:

0.2406102893950759

In [60]: 

newwdf.head()

Out[60]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

3 0.687101 0.153614 0.667723 0.347321 0.383641

4 0.734748 0.364787 0.310199 0.024578 0.456760

In [61]: 

newwdf.iloc[[0,1],[1,2]]

Out[61]:

B C

0 0.169806 0.801854

1 0.178549 0.611301

localhost:8889/notebooks/Untitled.ipynb 16/23
19/10/2020 Untitled - Jupyter Notebook

In [62]: 

newwdf.iloc[[0,5],[1,2]]

Out[62]:

B C

0 0.169806 0.801854

5 0.147095 0.811327

In [63]: 

newwdf.head(3)

Out[63]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

In [64]: 

drop1=newwdf.drop([3])
drop1.head()

Out[64]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.881627 0.178549 0.611301 0.318894 0.382391

2 0.347375 0.134880 0.978071 0.003456 0.042828

4 0.734748 0.364787 0.310199 0.024578 0.456760

5 0.148331 0.147095 0.811327 0.447947 0.407358

localhost:8889/notebooks/Untitled.ipynb 17/23
19/10/2020 Untitled - Jupyter Notebook

In [65]: 

drop2=newwdf.drop(['B'],axis=1)
drop2.head()

Out[65]:

A C D E

0 99 0.801854 0.264700 0.240610

1 0.881627 0.611301 0.318894 0.382391

2 0.347375 0.978071 0.003456 0.042828

3 0.687101 0.667723 0.347321 0.383641

4 0.734748 0.310199 0.024578 0.456760

In [66]: 

drop3=newwdf.drop(['A','C'],axis=1)
drop3.head()

Out[66]:

B D E

0 0.169806 0.264700 0.240610

1 0.178549 0.318894 0.382391

2 0.134880 0.003456 0.042828

3 0.153614 0.347321 0.383641

4 0.364787 0.024578 0.456760

In [67]: 

newwdf.drop([1,5], axis=0,inplace=True)

In [68]: 

newwdf.reset_index().head()

Out[68]:

index A B C D E

0 0 99 0.169806 0.801854 0.264700 0.240610

1 2 0.347375 0.134880 0.978071 0.003456 0.042828

2 3 0.687101 0.153614 0.667723 0.347321 0.383641

3 4 0.734748 0.364787 0.310199 0.024578 0.456760

4 6 0.498866 0.061017 0.166871 0.491814 0.377244

localhost:8889/notebooks/Untitled.ipynb 18/23
19/10/2020 Untitled - Jupyter Notebook

In [69]: 

newwdf.reset_index(drop=True).head()

Out[69]:

A B C D E

0 99 0.169806 0.801854 0.264700 0.240610

1 0.347375 0.134880 0.978071 0.003456 0.042828

2 0.687101 0.153614 0.667723 0.347321 0.383641

3 0.734748 0.364787 0.310199 0.024578 0.456760

4 0.498866 0.061017 0.166871 0.491814 0.377244

In [70]: 

newwdf['B'].isnull().head()

Out[70]:

0 False
2 False
3 False
4 False
6 False
Name: B, dtype: bool

In [71]: 

newwdf['B']=None

In [72]: 

newwdf.head()

Out[72]:

A B C D E

0 99 None 0.801854 0.264700 0.240610

2 0.347375 None 0.978071 0.003456 0.042828

3 0.687101 None 0.667723 0.347321 0.383641

4 0.734748 None 0.310199 0.024578 0.456760

6 0.498866 None 0.166871 0.491814 0.377244

localhost:8889/notebooks/Untitled.ipynb 19/23
19/10/2020 Untitled - Jupyter Notebook

In [73]: 

newwdf['B'].isnull().head()

Out[73]:

0 True
2 True
3 True
4 True
6 True
Name: B, dtype: bool

In [74]: 

newwdf.head()

Out[74]:

A B C D E

0 99 None 0.801854 0.264700 0.240610

2 0.347375 None 0.978071 0.003456 0.042828

3 0.687101 None 0.667723 0.347321 0.383641

4 0.734748 None 0.310199 0.024578 0.456760

6 0.498866 None 0.166871 0.491814 0.377244

In [75]: 

newwdf.loc[:,['B']]=None

In [76]: 

newwdf.head()

Out[76]:

A B C D E

0 99 None 0.801854 0.264700 0.240610

2 0.347375 None 0.978071 0.003456 0.042828

3 0.687101 None 0.667723 0.347321 0.383641

4 0.734748 None 0.310199 0.024578 0.456760

6 0.498866 None 0.166871 0.491814 0.377244

In [77]: 

newwdf.loc[:,['B']]=10

localhost:8889/notebooks/Untitled.ipynb 20/23
19/10/2020 Untitled - Jupyter Notebook

In [78]: 

newwdf.head()

Out[78]:

A B C D E

0 99 10 0.801854 0.264700 0.240610

2 0.347375 10 0.978071 0.003456 0.042828

3 0.687101 10 0.667723 0.347321 0.383641

4 0.734748 10 0.310199 0.024578 0.456760

6 0.498866 10 0.166871 0.491814 0.377244

In [79]: 

df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Alfred'],


"toy": [np.nan, 'NaN', 'Bullwhip'],
"born": [pd.NaT, pd.Timestamp("1940-04-25"),
pd.NaT]})

In [80]: 

df.head()

Out[80]:

name toy born

0 Alfred NaN NaT

1 Batman NaN 1940-04-25

2 Alfred Bullwhip NaT

In [81]: 

df.dropna(how='all',axis=1)

Out[81]:

name toy born

0 Alfred NaN NaT

1 Batman NaN 1940-04-25

2 Alfred Bullwhip NaT

localhost:8889/notebooks/Untitled.ipynb 21/23
19/10/2020 Untitled - Jupyter Notebook

In [82]: 

df.drop_duplicates(subset=['name'])

Out[82]:

name toy born

0 Alfred NaN NaT

1 Batman NaN 1940-04-25

In [83]: 

df.drop_duplicates(subset=['name'], keep=False)

Out[83]:

name toy born

1 Batman NaN 1940-04-25

In [84]: 

df.shape

Out[84]:

(3, 3)

In [85]: 

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 3 non-null object
1 toy 2 non-null object
2 born 1 non-null datetime64[ns]
dtypes: datetime64[ns](1), object(2)
memory usage: 200.0+ bytes

localhost:8889/notebooks/Untitled.ipynb 22/23
19/10/2020 Untitled - Jupyter Notebook

In [86]: 

df['name'].value_counts(dropna=False)

Out[86]:

Alfred 2
Batman 1
Name: name, dtype: int64

In [87]: 

df['toy'].value_counts(dropna=False)

Out[87]:

Bullwhip 1
NaN 1
NaN 1
Name: toy, dtype: int64

In [88]: 

df.isnull()

Out[88]:

name toy born

0 False True True

1 False False False

2 False False True

In [ ]: 

localhost:8889/notebooks/Untitled.ipynb 23/23

You might also like