Notesatfingerbyvrinda.files.wordpress.com
CHAPTER 3 – DATAFRAMECreation Of Dataframe From List Of Dictionaries:import pandas as pd data = [{'a': 1, 'b': 2, 'c':3}, {'a':10, 'b': 20, 'c': 30}] df1= pd.DataFrame(data) print(df1)df1['d']=[1,2]print("\n\n",df1) data = [{'a':1,'b': 2, 'c':3}, {'a': 10, 'b': 20, 'c': 30}] df2 = pd.DataFrame(data, index =['first', 'second']) print(df2)data = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}] df3 = pd.DataFrame(data, index =['first', 'second'], columns =['a', 'b']) df4 = pd.DataFrame(data, index =['first', 'second'], columns =['a', 'b1']) print (df3, "\n") print (df4)Creation Of Dataframe From Dictionary of Series:import pandas as pdd = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}df1 = pd.DataFrame(d)print(df1)#How to see the type of columns?print(df1.dtypes)#Selecting data column wiseprint("\n\nSelecting column one")print(df1['one'])# Adding a new column to an existing DataFrameprint("Method1")df1['three']=pd.Series([1,2,3,4],index=['a', 'b', 'c', 'd'])print("\n\nAfter adding column three")print(df1)print("Method2")df1.loc[:,'four']=[1,2,3,4]print("\n\nAfter adding column four using loc")print(df1)print("Method3")df1.at[:,'five']=[1,2,3,4]print("\n\nAfter adding column five using at")print(df1)print("Method4")df1=df1.assign(six=[1,2,3,4])print("\n\nAfter adding column six using assign")print(df1)#Deleting one columnprint("After deleting column three using pop")df1.pop('three')print(df1)print("After deleting column four using del")del df1['four']print(df1)#selecting row and modifying itprint(df1.loc['a'])print(df1.iloc[2])df1.loc[['a'],['two']]=55print("after modifying one data in the index a\n")print(df1)#slicing rowprint(df1[1:3])#adding new rowsd = {'one' : pd.Series([11, 22, 33], index=['a', 'b', 'c'])}df2 = pd.DataFrame(d)print(df2)df3=df1.append(df2)print("\n\nAfter appending\n",df3)#deleteing rowdf=df3.drop('a')print(df)#Renaming row and columndfnew=df1.rename(columns={'one':'ONE','two':'TWO'},index={'a':0})print(dfnew)#head and tail functionsprint(df1.head(1))print(df1.tail(1))print(df1) ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
Related searches
- wordpress passing data between pages
- wordpress business templates
- wordpress rss feed not working
- wordpress jquery is not defined
- create wordpress blog
- wordpress roles editor
- wordpress full rss feed
- wordpress rss feed settings
- wordpress rss feed plugin
- wordpress display rss feed
- wordpress rss feed link
- wordpress rss feed to post