CSL 410 L15
CSL 410 L15
Lecture No. 15
index
2 For the row labels, the Index to be used for the resulting frame is Optional Default np.arrange(n) if no
index is passed.
columns For column labels, the optional default syntax is -np.arrange(n). This is only trueif no index
3 is passed.
5 copy This command (or whatever it is) is used for copying of data, if the default is False.
• All the ndarrays must be of same length. If index is passed, then the length
of the index should equal to the length of the arrays. If no index is passed, then
by default, index will be range(n), where n is the array length.
• Example:
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df= pd.DataFrame(data)
print(df)
• Outcome:
Name Age
0 Tom 28
1 Jack 34
2 Steve 29
3 Ricky 42
• Outcome:
Note: Observe, df2 DataFrame is created with a column index other than
the dictionary key; thus, appended the NaN’s in place. Whereas, df1 is
created with column indices same as dictionary keys, so NaN’s appended.
• Observe, for the series one, there is no label ‘d’ passed, but in the result,
for the d label, NaN is appended with NaN.
• Column Selection:
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df ['one'])
• Outcome:
one
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
<SELO: 1> <Reference No.: R1,R4>
DataFrame : Create a DataFrame from Dict of Series
Column Addition:
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
# Adding a new column to an existing DataFrame object with column labe
l by passing new series
print ("Adding a new column by passing as Series:")
df['three']=pd.Series([10,20,30],index=['a','b','c'])
print(df)
print ("Adding a new column using the existing columns in DataFrame:")
df['four']=df['one']+df['three']
print(df)
<SELO: 1> <Reference No.: R1,R4>
DataFrame : Create a DataFrame from Dict of Series
Column Addition:
Column Deletion:
# Using the previous DataFrame, we will delete a column
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
'three' : pd.Series([10,20,30], index=['a','b','c'])}
df = pd.DataFrame(d)
print ("Our dataframe is:")
print (df)
# using del function
print ("Deleting the first column using DEL function:")
del df['one']
print (df)
# using pop function
print ("Deleting another column using POP function:")
df.pop('two')
print (df)
Column Deletion: