SlideShare a Scribd company logo
1 of 1
Download to read offline
PythonForDataScience Cheat Sheet
Pandas
Learn Python for Data Science Interactively at www.DataCamp.com
Reshaping Data
DataCamp
Learn Python for Data Science Interactively
Advanced Indexing
Reindexing
>>> s2 = s.reindex(['a','c','d','e','b'])
>>> s3 = s.reindex(range(5),
	 method='bfill')
0 3
1 3
2 3
3 3
4 3
Forward Filling Backward Filling
>>> df.reindex(range(4),
	 method='ffill')
Country Capital Population
0 Belgium Brussels 11190846
1 India New Delhi 1303171035
2 Brazil Brasília 207847528
3 Brazil Brasília 207847528
Pivot
Stack / Unstack
Melt
Combining Data
>>> pd.melt(df2, Gather columns into rows
id_vars=["Date"],
value_vars=["Type", "Value"],
value_name="Observations")
>>> stacked = df5.stack() Pivot a level of column labels
>>> stacked.unstack() Pivot a level of index labels
>>> df3= df2.pivot(index='Date', Spread rows into columns
columns='Type',
values='Value')
>>> arrays = [np.array([1,2,3]),
np.array([5,4,3])]
>>> df5 = pd.DataFrame(np.random.rand(3, 2), index=arrays)
>>> tuples = list(zip(*arrays))
>>> index = pd.MultiIndex.from_tuples(tuples,
names=['first', 'second'])
>>> df6 = pd.DataFrame(np.random.rand(3, 2), index=index)
>>> df2.set_index(["Date", "Type"])
Missing Data
>>> df.dropna() Drop NaN values
>>> df3.fillna(df3.mean()) Fill NaN values with a predetermined value
>>> df2.replace("a", "f") Replace values with others
2016-03-01 a
2016-03-02 b
2016-03-01 c
11.432
13.031
20.784
2016-03-03 a
2016-03-02 a
2016-03-03 c
99.906
1.303
20.784
Date Type Value
0
1
2
3
4
5
Type
Date
2016-03-01
2016-03-02
2016-03-03
a
11.432
1.303
99.906
b
NaN
13.031
NaN
c
20.784
NaN
20.784
Selecting
>>> df3.loc[:,(df3>1).any()] Select cols with any vals >1
>>> df3.loc[:,(df3>1).all()] Select cols with vals > 1
>>> df3.loc[:,df3.isnull().any()] Select cols with NaN
>>> df3.loc[:,df3.notnull().all()] Select cols without NaN
Indexing With isin
>>> df[(df.Country.isin(df2.Type))] Find same elements
>>> df3.filter(items=”a”,”b”]) Filter on values
>>> df.select(lambda x: not x%5) Select specific elements
Where
>>> s.where(s > 0) Subset the data
Query
>>> df6.query('second > first') Query DataFrame
Pivot Table
>>> df4 = pd.pivot_table(df2, Spread rows into columns
values='Value',
index='Date',
columns='Type'])
Merge
Join
Concatenate
>>> pd.merge(data1,
data2,
how='left',
on='X1')
>>> data1.join(data2, how='right')
Vertical
>>> s.append(s2)
Horizontal/Vertical
>>> pd.concat([s,s2],axis=1, keys=['One','Two'])
>>> pd.concat([data1, data2], axis=1, join='inner')
1
2
3
5
4
3
0
1
0
1
0
1
0.233482
0.390959
0.184713
0.237102
0.433522
0.429401
1
2
3
5
4
3
0
0.233482
0.184713
0.433522
1
0.390959
0.237102
0.429401
Stacked
Unstacked
2016-03-01 a
2016-03-02 b
2016-03-01 c
11.432
13.031
20.784
2016-03-03 a
2016-03-02 a
2016-03-03 c
99.906
1.303
20.784
Date Type Value
0
1
2
3
4
5
2016-03-01 Type
2016-03-02 Type
2016-03-01 Type
a
b
c
2016-03-03 Type
2016-03-02 Type
2016-03-03 Type
a
a
c
Date Variable Observations
0
1
2
3
4
5
2016-03-01 Value
2016-03-02 Value
2016-03-01 Value
11.432
13.031
20.784
2016-03-03 Value
2016-03-02 Value
2016-03-03 Value
99.906
1.303
20.784
6
7
8
9
10
11
Iteration
>>> df.iteritems() (Column-index, Series) pairs
>>> df.iterrows() (Row-index, Series) pairs
data1
a
b
c
11.432
1.303
99.906
X1 X2
a
b
d
20.784
NaN
20.784
data2
X1 X3
a
b
c
11.432
1.303
99.906
20.784
NaN
NaN
X1 X2 X3
>>> pd.merge(data1,
data2,
how='outer',
on='X1')
>>> pd.merge(data1,
data2,
how='right',
on='X1')
a
b
d
11.432
1.303
NaN
20.784
NaN
20.784
X1 X2 X3
>>> pd.merge(data1,
data2,
how='inner',
on='X1')
a
b
11.432
1.303
20.784
NaN
X1 X2 X3
a
b
c
11.432
1.303
99.906
20.784
NaN
NaN
X1 X2 X3
d NaN 20.784
Setting/Resetting Index
>>> df.set_index('Country') Set the index
>>> df4 = df.reset_index() Reset the index
>>> df = df.rename(index=str, Rename DataFrame
columns={"Country":"cntry",
"Capital":"cptl",
"Population":"ppltn"})
Duplicate Data
>>> s3.unique() Return unique values
>>> df2.duplicated('Type') Check duplicates
>>> df2.drop_duplicates('Type', keep='last') Drop duplicates
>>> df.index.duplicated() Check index duplicates
Grouping Data
Aggregation
>>> df2.groupby(by=['Date','Type']).mean()
>>> df4.groupby(level=0).sum()
>>> df4.groupby(level=0).agg({'a':lambda x:sum(x)/len(x),
'b': np.sum})
Transformation
>>> customSum = lambda x: (x+x%2)
>>> df4.groupby(level=0).transform(customSum)
MultiIndexing
Dates
Visualization
Also see NumPy Arrays
>>> s.plot()
>>> plt.show()
Also see Matplotlib
>>> import matplotlib.pyplot as plt
>>> df2.plot()
>>> plt.show()
>>> df2['Date']= pd.to_datetime(df2['Date'])
>>> df2['Date']= pd.date_range('2000-1-1',
periods=6,
freq='M')
>>> dates = [datetime(2012,5,1), datetime(2012,5,2)]
>>> index = pd.DatetimeIndex(dates)
>>> index = pd.date_range(datetime(2012,2,1), end, freq='BM')

More Related Content

What's hot

Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascienceNishant Upadhyay
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization Sourabh Sahu
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examplesDennis
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in REshwar Sai
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Parth Khare
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat SheetACASH1011
 
Rewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasRewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasAnatolii Kmetiuk
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
Webi Report Function Overview
Webi Report Function OverviewWebi Report Function Overview
Webi Report Function OverviewSrinath Reddy
 

What's hot (18)

Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examples
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
R programming
R programmingR programming
R programming
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
R programming language
R programming languageR programming language
R programming language
 
Dplyr and Plyr
Dplyr and PlyrDplyr and Plyr
Dplyr and Plyr
 
Rewriting Engine for Process Algebras
Rewriting Engine for Process AlgebrasRewriting Engine for Process Algebras
Rewriting Engine for Process Algebras
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
Webi Report Function Overview
Webi Report Function OverviewWebi Report Function Overview
Webi Report Function Overview
 
Rsplit apply combine
Rsplit apply combineRsplit apply combine
Rsplit apply combine
 

Similar to 3 pandasadvanced

Presentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptxPresentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptx16115yogendraSingh
 
pandas dataframe notes.pdf
pandas dataframe notes.pdfpandas dataframe notes.pdf
pandas dataframe notes.pdfAjeshSurejan2
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSigmoid
 
Cheatsheet_Python.pdf
Cheatsheet_Python.pdfCheatsheet_Python.pdf
Cheatsheet_Python.pdfRamyaR163211
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxprakashvs7
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)EMRE AKCAOGLU
 
Working with Graphs _python.pptx
Working with Graphs _python.pptxWorking with Graphs _python.pptx
Working with Graphs _python.pptxMrPrathapG
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxSovannDoeur
 
pyspark_df.pdf
pyspark_df.pdfpyspark_df.pdf
pyspark_df.pdfSJain36
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandasPiyush rai
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheetNishant Upadhyay
 
Getting started with Pandas Cheatsheet.pdf
Getting started with Pandas Cheatsheet.pdfGetting started with Pandas Cheatsheet.pdf
Getting started with Pandas Cheatsheet.pdfSudhakarVenkey
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfRahul Jain
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisSpbDotNet Community
 

Similar to 3 pandasadvanced (20)

2 pandasbasic
2 pandasbasic2 pandasbasic
2 pandasbasic
 
Presentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptxPresentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptx
 
pandas dataframe notes.pdf
pandas dataframe notes.pdfpandas dataframe notes.pdf
pandas dataframe notes.pdf
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. Jyotiska
 
Cheatsheet_Python.pdf
Cheatsheet_Python.pdfCheatsheet_Python.pdf
Cheatsheet_Python.pdf
 
Pandas.ppt
Pandas.pptPandas.ppt
Pandas.ppt
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptx
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)DataCamp Cheat Sheets 4 Python Users (2020)
DataCamp Cheat Sheets 4 Python Users (2020)
 
Working with Graphs _python.pptx
Working with Graphs _python.pptxWorking with Graphs _python.pptx
Working with Graphs _python.pptx
 
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptxChapter 3 Built-in Data Structures, Functions, and Files .pptx
Chapter 3 Built-in Data Structures, Functions, and Files .pptx
 
pyspark_df.pdf
pyspark_df.pdfpyspark_df.pdf
pyspark_df.pdf
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 
ppanda.pptx
ppanda.pptxppanda.pptx
ppanda.pptx
 
Getting started with Pandas Cheatsheet.pdf
Getting started with Pandas Cheatsheet.pdfGetting started with Pandas Cheatsheet.pdf
Getting started with Pandas Cheatsheet.pdf
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdf
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
Python for R users
Python for R usersPython for R users
Python for R users
 

More from pramod naik

More from pramod naik (12)

Electrical lab
Electrical labElectrical lab
Electrical lab
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Adversarial search
Adversarial searchAdversarial search
Adversarial search
 
1 pythonbasic
1 pythonbasic1 pythonbasic
1 pythonbasic
 
Chapter07
Chapter07Chapter07
Chapter07
 
Chapter06
Chapter06Chapter06
Chapter06
 
Chapter05
Chapter05Chapter05
Chapter05
 
Chapter04b
Chapter04bChapter04b
Chapter04b
 
Chapter04a
Chapter04aChapter04a
Chapter04a
 
Chapter01
Chapter01Chapter01
Chapter01
 
Ilsvrc2015 deep residual_learning_kaiminghe
Ilsvrc2015 deep residual_learning_kaimingheIlsvrc2015 deep residual_learning_kaiminghe
Ilsvrc2015 deep residual_learning_kaiminghe
 
Ganesan dhawanrpt
Ganesan dhawanrptGanesan dhawanrpt
Ganesan dhawanrpt
 

Recently uploaded

NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...
NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...
NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...Amil Baba Dawood bangali
 
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一Fi sss
 
Planning your Restaurant's Path to Profitability
Planning your Restaurant's Path to ProfitabilityPlanning your Restaurant's Path to Profitability
Planning your Restaurant's Path to ProfitabilityAggregage
 
Irradiation preservation of food advancements
Irradiation preservation of food advancementsIrradiation preservation of food advancements
Irradiation preservation of food advancementsDeepika Sugumar
 
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...dollysharma2066
 
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girls
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call GirlsAbu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girls
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girlstiril72860
 
526350093-Online-Food-Ordering-System-Ppt.pptx
526350093-Online-Food-Ordering-System-Ppt.pptx526350093-Online-Food-Ordering-System-Ppt.pptx
526350093-Online-Food-Ordering-System-Ppt.pptxJaidBagwan2
 
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?t6tjlrih
 
HIGH PRESSURE PROCESSING ( HPP ) .pptx
HIGH PRESSURE  PROCESSING ( HPP )  .pptxHIGH PRESSURE  PROCESSING ( HPP )  .pptx
HIGH PRESSURE PROCESSING ( HPP ) .pptxparvin6647
 
Food-Allergy-PowerPoint-Presentation-2.ppt
Food-Allergy-PowerPoint-Presentation-2.pptFood-Allergy-PowerPoint-Presentation-2.ppt
Food-Allergy-PowerPoint-Presentation-2.pptIsaacMensah62
 
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptx
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptxACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptx
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptxBELARMINOJOLINA
 
pitch presentation B2.pptx Sunderland Culture
pitch presentation B2.pptx Sunderland Culturepitch presentation B2.pptx Sunderland Culture
pitch presentation B2.pptx Sunderland CultureChloeMeadows1
 
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012rehmti665
 
Prepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II ModulePrepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II Modulemaricel769799
 
韩国学位证,全北大学毕业证书1:1制作
韩国学位证,全北大学毕业证书1:1制作韩国学位证,全北大学毕业证书1:1制作
韩国学位证,全北大学毕业证书1:1制作7tz4rjpd
 
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATFUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATBHIKHUKUMAR KUNWARADIYA
 
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...dollysharma2066
 
Estimation of protein quality using various methods
Estimation of protein quality using various methodsEstimation of protein quality using various methods
Estimation of protein quality using various methodsThiviKutty
 

Recently uploaded (20)

NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...
NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...
NO1 WorldWide kala jadu Love Marriage Black Magic Punjab Powerful Black Magic...
 
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
(办理学位证)加州大学圣塔芭芭拉分校毕业证成绩单原版一比一
 
Planning your Restaurant's Path to Profitability
Planning your Restaurant's Path to ProfitabilityPlanning your Restaurant's Path to Profitability
Planning your Restaurant's Path to Profitability
 
Irradiation preservation of food advancements
Irradiation preservation of food advancementsIrradiation preservation of food advancements
Irradiation preservation of food advancements
 
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
Russian Escorts DELHI - Russian Call Girls in Delhi Greater Kailash TELL-NO. ...
 
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girls
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call GirlsAbu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girls
Abu Dhabi Housewife Call Girls +971509530047 Abu Dhabi Call Girls
 
526350093-Online-Food-Ordering-System-Ppt.pptx
526350093-Online-Food-Ordering-System-Ppt.pptx526350093-Online-Food-Ordering-System-Ppt.pptx
526350093-Online-Food-Ordering-System-Ppt.pptx
 
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
如何办韩国SKKU文凭,成均馆大学毕业证学位证怎么辨别?
 
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
 
HIGH PRESSURE PROCESSING ( HPP ) .pptx
HIGH PRESSURE  PROCESSING ( HPP )  .pptxHIGH PRESSURE  PROCESSING ( HPP )  .pptx
HIGH PRESSURE PROCESSING ( HPP ) .pptx
 
Food-Allergy-PowerPoint-Presentation-2.ppt
Food-Allergy-PowerPoint-Presentation-2.pptFood-Allergy-PowerPoint-Presentation-2.ppt
Food-Allergy-PowerPoint-Presentation-2.ppt
 
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptx
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptxACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptx
ACCEPTABILITY-OF-AMPALAYA-BITTER-GOURD.pptx
 
pitch presentation B2.pptx Sunderland Culture
pitch presentation B2.pptx Sunderland Culturepitch presentation B2.pptx Sunderland Culture
pitch presentation B2.pptx Sunderland Culture
 
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012
Call Girls Laxmi Nagar Delhi reach out to us at ☎ 9711199012
 
Prepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II ModulePrepare And Cook Meat.pptx Quarter II Module
Prepare And Cook Meat.pptx Quarter II Module
 
韩国学位证,全北大学毕业证书1:1制作
韩国学位证,全北大学毕业证书1:1制作韩国学位证,全北大学毕业证书1:1制作
韩国学位证,全北大学毕业证书1:1制作
 
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THATFUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
FUTURISTIC FOOD PRODUCTS OFTEN INVOLVE INNOVATIONS THAT
 
Call Girls in Hauz Khas⎝⎝9953056974⎝⎝ Delhi NCR
Call Girls in Hauz Khas⎝⎝9953056974⎝⎝ Delhi NCRCall Girls in Hauz Khas⎝⎝9953056974⎝⎝ Delhi NCR
Call Girls in Hauz Khas⎝⎝9953056974⎝⎝ Delhi NCR
 
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...
Affordable PriceD Call Girls In Crowne Plaza Greater Noida 8377877756 Short 2...
 
Estimation of protein quality using various methods
Estimation of protein quality using various methodsEstimation of protein quality using various methods
Estimation of protein quality using various methods
 

3 pandasadvanced

  • 1. PythonForDataScience Cheat Sheet Pandas Learn Python for Data Science Interactively at www.DataCamp.com Reshaping Data DataCamp Learn Python for Data Science Interactively Advanced Indexing Reindexing >>> s2 = s.reindex(['a','c','d','e','b']) >>> s3 = s.reindex(range(5), method='bfill') 0 3 1 3 2 3 3 3 4 3 Forward Filling Backward Filling >>> df.reindex(range(4), method='ffill') Country Capital Population 0 Belgium Brussels 11190846 1 India New Delhi 1303171035 2 Brazil Brasília 207847528 3 Brazil Brasília 207847528 Pivot Stack / Unstack Melt Combining Data >>> pd.melt(df2, Gather columns into rows id_vars=["Date"], value_vars=["Type", "Value"], value_name="Observations") >>> stacked = df5.stack() Pivot a level of column labels >>> stacked.unstack() Pivot a level of index labels >>> df3= df2.pivot(index='Date', Spread rows into columns columns='Type', values='Value') >>> arrays = [np.array([1,2,3]), np.array([5,4,3])] >>> df5 = pd.DataFrame(np.random.rand(3, 2), index=arrays) >>> tuples = list(zip(*arrays)) >>> index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) >>> df6 = pd.DataFrame(np.random.rand(3, 2), index=index) >>> df2.set_index(["Date", "Type"]) Missing Data >>> df.dropna() Drop NaN values >>> df3.fillna(df3.mean()) Fill NaN values with a predetermined value >>> df2.replace("a", "f") Replace values with others 2016-03-01 a 2016-03-02 b 2016-03-01 c 11.432 13.031 20.784 2016-03-03 a 2016-03-02 a 2016-03-03 c 99.906 1.303 20.784 Date Type Value 0 1 2 3 4 5 Type Date 2016-03-01 2016-03-02 2016-03-03 a 11.432 1.303 99.906 b NaN 13.031 NaN c 20.784 NaN 20.784 Selecting >>> df3.loc[:,(df3>1).any()] Select cols with any vals >1 >>> df3.loc[:,(df3>1).all()] Select cols with vals > 1 >>> df3.loc[:,df3.isnull().any()] Select cols with NaN >>> df3.loc[:,df3.notnull().all()] Select cols without NaN Indexing With isin >>> df[(df.Country.isin(df2.Type))] Find same elements >>> df3.filter(items=”a”,”b”]) Filter on values >>> df.select(lambda x: not x%5) Select specific elements Where >>> s.where(s > 0) Subset the data Query >>> df6.query('second > first') Query DataFrame Pivot Table >>> df4 = pd.pivot_table(df2, Spread rows into columns values='Value', index='Date', columns='Type']) Merge Join Concatenate >>> pd.merge(data1, data2, how='left', on='X1') >>> data1.join(data2, how='right') Vertical >>> s.append(s2) Horizontal/Vertical >>> pd.concat([s,s2],axis=1, keys=['One','Two']) >>> pd.concat([data1, data2], axis=1, join='inner') 1 2 3 5 4 3 0 1 0 1 0 1 0.233482 0.390959 0.184713 0.237102 0.433522 0.429401 1 2 3 5 4 3 0 0.233482 0.184713 0.433522 1 0.390959 0.237102 0.429401 Stacked Unstacked 2016-03-01 a 2016-03-02 b 2016-03-01 c 11.432 13.031 20.784 2016-03-03 a 2016-03-02 a 2016-03-03 c 99.906 1.303 20.784 Date Type Value 0 1 2 3 4 5 2016-03-01 Type 2016-03-02 Type 2016-03-01 Type a b c 2016-03-03 Type 2016-03-02 Type 2016-03-03 Type a a c Date Variable Observations 0 1 2 3 4 5 2016-03-01 Value 2016-03-02 Value 2016-03-01 Value 11.432 13.031 20.784 2016-03-03 Value 2016-03-02 Value 2016-03-03 Value 99.906 1.303 20.784 6 7 8 9 10 11 Iteration >>> df.iteritems() (Column-index, Series) pairs >>> df.iterrows() (Row-index, Series) pairs data1 a b c 11.432 1.303 99.906 X1 X2 a b d 20.784 NaN 20.784 data2 X1 X3 a b c 11.432 1.303 99.906 20.784 NaN NaN X1 X2 X3 >>> pd.merge(data1, data2, how='outer', on='X1') >>> pd.merge(data1, data2, how='right', on='X1') a b d 11.432 1.303 NaN 20.784 NaN 20.784 X1 X2 X3 >>> pd.merge(data1, data2, how='inner', on='X1') a b 11.432 1.303 20.784 NaN X1 X2 X3 a b c 11.432 1.303 99.906 20.784 NaN NaN X1 X2 X3 d NaN 20.784 Setting/Resetting Index >>> df.set_index('Country') Set the index >>> df4 = df.reset_index() Reset the index >>> df = df.rename(index=str, Rename DataFrame columns={"Country":"cntry", "Capital":"cptl", "Population":"ppltn"}) Duplicate Data >>> s3.unique() Return unique values >>> df2.duplicated('Type') Check duplicates >>> df2.drop_duplicates('Type', keep='last') Drop duplicates >>> df.index.duplicated() Check index duplicates Grouping Data Aggregation >>> df2.groupby(by=['Date','Type']).mean() >>> df4.groupby(level=0).sum() >>> df4.groupby(level=0).agg({'a':lambda x:sum(x)/len(x), 'b': np.sum}) Transformation >>> customSum = lambda x: (x+x%2) >>> df4.groupby(level=0).transform(customSum) MultiIndexing Dates Visualization Also see NumPy Arrays >>> s.plot() >>> plt.show() Also see Matplotlib >>> import matplotlib.pyplot as plt >>> df2.plot() >>> plt.show() >>> df2['Date']= pd.to_datetime(df2['Date']) >>> df2['Date']= pd.date_range('2000-1-1', periods=6, freq='M') >>> dates = [datetime(2012,5,1), datetime(2012,5,2)] >>> index = pd.DatetimeIndex(dates) >>> index = pd.date_range(datetime(2012,2,1), end, freq='BM')