Important pandas Data Frame attribute Class 12 Code: 065
import pandas as pd
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
print()
print("Head function calling head(2)\n",df.head(2))
print()
print("Head function calling head(n=2)\n",df.head(n=2))
print()
print("Head function calling head()\n",df.head())
print()
print("Head function calling tail()\n",df.tail())
print()
print("Head function calling tail(2)\n",df.tail(2))
OUT PUT
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
Head function calling head(2)
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
Head function calling head(n=2)
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
Head function calling head()
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
Head function calling tail()
name city age py-score
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
Head function calling tail(2)
name city age py-score
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
-----------------------------------------------------------------------------------------------------
import pandas as pd
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
'''print()
print("Head function calling head(2)\n",df.head(2))
print()
print("Head function calling head(n=2)\n",df.head(n=2))
print()
print("Head function calling head()\n",df.head())
print()
print("Head function calling tail()\n",df.tail())
print()
print("Head function calling tail(2)\n",df.tail(2))'''
print("\nData manipulatation \n")
cities = df['city']
print(cities)
print("\nData manipulatation df.city \n")
print(df.city)
Out Put
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
Data manipulatation
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Data manipulatation df.city
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
-------------------------------------------------------------------------------------------------------------------------------
import pandas as pd
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
#citi = df['city']
print(cities)
print("\nData manipulatation df.city \n")
print(df.city)
print(citi[102])
print(df.loc[103])
print(df.at[101,'name'])
print(df.at[105,'age'])
Out Put of this code
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Data manipulatation df.city
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Toronto
name Jana
city Prague
age 33
py-score 81
Name: 103, dtype: object
Xavier
38
------------------------------------------------------------------------------------------
import pandas as pdd = {'x': [1, 2, 3,4], 'y': np.array([2, 4, 8,10]), 'z': 100}df=pd.DataFrame(d)print(df)# second method to define Dataframedf2=pd.DataFrame(d, index=[100, 200, 300,400], columns=['z', 'y', 'x','p'])df3=pd.DataFrame([[1,2,3],[2,4,5],[100,100,100]], index=[100, 200, 300], columns=['z', 'y', 'x'])print("given coloum name\n\n",df2)print("\n\ngiven coloum name\n\n",df3)l = [[1, 2, 100],[2, 4, 100],[3, 8, 100]]df4=pd.df2=pd.DataFrame(l, index=[100, 200, 300], columns=['x', 'y', 'z'])print("\n\ngiven coloum name\n\n",df4)print("index=",df.index)print("value=",df.values)print("coloums=",df.columns)print("\ndf.columns=",df.columns[0])print("\n data type=",df.dtypes)print("ndim=",df.ndim)print("shape=",df.shape)print("size=",df.size)#print("Memory usage=",df.memory_usage())OUT PUT.x y z 0 1 2 100 1 2 4 100 2 3 8 100 3 4 10 100 given coloum name z y x p 100 100 2 1 NaN 200 100 4 2 NaN 300 100 8 3 NaN 400 100 10 4 NaN given coloum name z y x 100 1 2 3 200 2 4 5 300 100 100 100 given coloum name x y z 100 1 2 100 200 2 4 100 300 3 8 100 index= RangeIndex(start=0, stop=4, step=1) value= [[ 1 2 100] [ 2 4 100] [ 3 8 100] [ 4 10 100]] coloums= Index(['x', 'y', 'z'], dtype='object') df.columns= x data type= x int64 y int64 z int64 dtype: object ndim= 2 shape= (4, 3) size= 12 Memory usage= Index 128 x 32 y 32 z 32 dtype: int64-----------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
Pandas has four accessors [.loc, iloc, .at. iat] in used to access data from DataFrame in pandas. All attributes and their syntax are discussed in this article and we also discussed output of these attributes we also put some line of code and discussed output of the particular code. You just read this article and you can easily these [.loc, iloc, .at. iat] attributes
- .loc[] accepts the labels of rows and columns and returns Series or DataFrames.You can use it to get entire rows or columns, as well as their parts.
- .iloc[] accepts the zero-based indices of rows and columns and returns Series or DataFrames. You can use it to get entire rows or columns, or their parts.
- .at[] accepts the labels of rows and columns and returns a single data value.
- .iat[] accepts the zero-based indices of rows and columns and returns a single data value. '''
Code part
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
'''
Pandas has four accessors in total:
.loc[] accepts the labels of rows and columns and returns Series or
DataFrames.You can use it to get entire rows or columns, as well as their parts.
.iloc[] accepts the zero-based indices of rows and columns and returns Series
or DataFrames. You can use it to get entire rows or columns, or their parts.
.at[] accepts the labels of rows and columns and returns a single data value.
.iat[] accepts the zero-based indices of rows and columns and returns a single
data value.
'''
cities = df['city']
print("cities = df['city']",cities)#show output as the city columns
print("\nData manipulatation df.city \n")
print(df.city)#show output as the city columns
print(cities[102]) #show output as the city columns
print(df.loc[103]) # show output as the 103 row
print("\n\ndf.at[101,'name']\n",df.at[101,'name'])#show output as the 101 row with name columns value Xavier
print("\n\ndf.at[105,'age']\n",df.at[105,'age']) #show output as the 105 row with age columns value 38
print("\n\ndf.loc[103]\n",df.loc[103])# show out put as index value 103 complete row
print("\n\ndf.iloc[0]\n",df.iloc[0]) # show output as index 0 complete row
print("\n\ndf.loc[:, 'city']\n",df.loc[:, 'city']) #show output all row and city columns
print("\n\ndf.loc[:, 'city':]\n",df.loc[:, 'city':])#show output all row and city columns and after all columns
print("\n\ndf.loc[:, 'city']\n",df.loc[:, :'city']) # show output all rows and city and previous columns
print("\n\ndf.loc[101:, ]\n",df.loc[101:, :]) #show output 101 and all row and all columsn
print("\n\ndf.loc[:105, 'city']\n",df.loc[:105, :'city']) #show output 101 to 105 abd all columns before city and city also
print("\n\ndf.loc[:5, 'city']\n",df.loc[:5, :'city']) #show output as empty dataframe
print("\n\ndf.loc[1:, 'city']\n",df.iloc[1:6, 0:1]) #show 102 to 106 rows with name columns
print("\n\noutput df.iloc[1:6:2, 1]",df.iloc[1:6:2, 1]) #show 102 104, 106 row and city columns
print("\n\ndf.iloc[pd.IndexSlice[1:6:2], 0]",df.iloc[pd.IndexSlice[1:6:2], 0])
john = pd.Series(data=['John', 'Boston', 34, 79],index=df.columns, name=107)
Out Put of this code
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
cities = df['city'] 101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Data manipulatation df.city
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Toronto
name Jana
city Prague
age 33
py-score 81
Name: 103, dtype: object
df.at[101,'name']
Xavier
df.at[105,'age']
38
df.loc[103]
name Jana
city Prague
age 33
py-score 81
Name: 103, dtype: object
df.iloc[0]
name Xavier
city Mexico City
age 41
py-score 88
Name: 101, dtype: object
df.loc[:, 'city']
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
df.loc[:, 'city':]
city age py-score
101 Mexico City 41 88.0
102 Toronto 28 79.0
103 Prague 33 81.0
104 Shanghai 34 80.0
105 Manchester 38 68.0
106 Cairo 31 61.0
107 Osaka 37 84.0
df.loc[:, 'city']
name city
101 Xavier Mexico City
102 Ann Toronto
103 Jana Prague
104 Yi Shanghai
105 Robin Manchester
106 Amal Cairo
107 Nori Osaka
df.loc[101:, ]
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
df.loc[:105, 'city']
name city
101 Xavier Mexico City
102 Ann Toronto
103 Jana Prague
104 Yi Shanghai
105 Robin Manchester
df.loc[:5, 'city']
Empty DataFrame
Columns: [name, city]
Index: []
df.loc[1:, 'city']
name
102 Ann
103 Jana
104 Yi
105 Robin
106 Amal
---------------------------------------------------------------------------------------------------------------------------
Data modification and deletion and insertion:
You can add a new row to the end of
df
with .append()
:and you can delete it with a single call to
.drop()
:You can also remove one or more columns with
.drop()
as you did previously with the rows. Again, you need to specify the labels of the desired columns with labels
. In addition, when you want to remove columns, you need to provide the argument axis=1
: if you want to remove more than 1 columns you needs to sent labels as a list of columns name to which you want to be deleteif you want to delete a row or multiple row pass the index to the labels and axis =0 as shown in syntax
df.drop(labels=[101,103], axis=0)
import pandas as pd
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
cities = df['city']
print("cities = df['city']",cities)#show output as the city columns
print("\nData manipulatation df.city \n")
print(df.city)#show output as the city columns
print(cities[102]) #show output as the city columns
print(df.loc[103]) # show output as the 103 row
print("\n\ndf.at[101,'name']\n",df.at[101,'name'])#show output as the 101 row with name columns value Xavier
print("\n\ndf.at[105,'age']\n",df.at[105,'age']) #show output as the 105 row with age columns value 38
print("\n\ndf.loc[103]\n",df.loc[103])# show out put as index value 103 complete row
print("\n\ndf.iloc[0]\n",df.iloc[0]) # show output as index 0 complete row
print("\n\ndf.loc[:, 'city']\n",df.loc[:, 'city']) #show output all row and city columns
print("\n\ndf.loc[:, 'city':]\n",df.loc[:, 'city':])#show output all row and city columns and after all columns
print("\n\ndf.loc[:, 'city']\n",df.loc[:, :'city']) # show output all rows and city and previous columns
print("\n\ndf.loc[101:, ]\n",df.loc[101:, :]) #show output 101 and all row and all columsn
print("\n\ndf.loc[:105, 'city']\n",df.loc[:105, :'city']) #show output 101 to 105 abd all columns before city and city also
print("\n\ndf.loc[:5, 'city']\n",df.loc[:5, :'city']) #show output as empty dataframe
print("\n\ndf.loc[1:, 'city']\n",df.iloc[1:6, 0:1]) #show 102 to 106 rows with name columns
print("\n\noutput df.iloc[1:6:2, 1]",df.iloc[1:6:2, 1])
print("\n\ndf.at[102, 'name']",df.at[102, 'name'])# if you want to access particular data we need at attribute
print("\n\ndf.iat[2, 0]",df.iat[2, 0])# if you want to access particular data we need at attribute
# data modification
df12=df.loc[:103, 'py-score'] = [40, 50, 60]
df13=df.loc[104:, 'py-score'] = 0
print("\n\nScore after modification\n",df)
john = pd.Series(data=['John', 'Boston', 34, 79],index=df.columns, name=108)
print(john)
df111=df.append(john)
print("\n\nafter append funtion\n",df111)
df111.drop(labels=[107],inplace=True)
df['total-score'] = 0.0
df['js-score'] = [12,12,12,34,56,78,90]
print("\n\nafter adding new columns\n",df)
#print(df.insert(loc=4, column='django-score',value=[86.0, 81.0, 78.0, 88.0, 74.0, 70.0,87.0]))
del df['name']
print("\n\nafter deleting name columns\n",df)
df = df.drop(labels='age', axis=1)
print("\n\nafter drop function implementation\n",df)
Output
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
cities = df['city'] 101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Data manipulatation df.city
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
Toronto
name Jana
city Prague
age 33
py-score 81
Name: 103, dtype: object
df.at[101,'name']
Xavier
df.at[105,'age']
38
df.loc[103]
name Jana
city Prague
age 33
py-score 81
Name: 103, dtype: object
df.iloc[0]
name Xavier
city Mexico City
age 41
py-score 88
Name: 101, dtype: object
df.loc[:, 'city']
101 Mexico City
102 Toronto
103 Prague
104 Shanghai
105 Manchester
106 Cairo
107 Osaka
Name: city, dtype: object
df.loc[:, 'city':]
city age py-score
101 Mexico City 41 88.0
102 Toronto 28 79.0
103 Prague 33 81.0
104 Shanghai 34 80.0
105 Manchester 38 68.0
106 Cairo 31 61.0
107 Osaka 37 84.0
df.loc[:, 'city']
name city
101 Xavier Mexico City
102 Ann Toronto
103 Jana Prague
104 Yi Shanghai
105 Robin Manchester
106 Amal Cairo
107 Nori Osaka
df.loc[101:, ]
name city age py-score
101 Xavier Mexico City 41 88.0
102 Ann Toronto 28 79.0
103 Jana Prague 33 81.0
104 Yi Shanghai 34 80.0
105 Robin Manchester 38 68.0
106 Amal Cairo 31 61.0
107 Nori Osaka 37 84.0
df.loc[:105, 'city']
name city
101 Xavier Mexico City
102 Ann Toronto
103 Jana Prague
104 Yi Shanghai
105 Robin Manchester
df.loc[:5, 'city']
Empty DataFrame
Columns: [name, city]
Index: []
df.loc[1:, 'city']
name
102 Ann
103 Jana
104 Yi
105 Robin
106 Amal
output df.iloc[1:6:2, 1] 102 Toronto
104 Shanghai
106 Cairo
Name: city, dtype: object
df.at[102, 'name'] Ann
df.iat[2, 0] Jana
Score after modification
name city age py-score
101 Xavier Mexico City 41 40.0
102 Ann Toronto 28 50.0
103 Jana Prague 33 60.0
104 Yi Shanghai 34 0.0
105 Robin Manchester 38 0.0
106 Amal Cairo 31 0.0
107 Nori Osaka 37 0.0
name John
city Boston
age 34
py-score 79
Name: 108, dtype: object
after append funtion
name city age py-score
101 Xavier Mexico City 41 40.0
102 Ann Toronto 28 50.0
103 Jana Prague 33 60.0
104 Yi Shanghai 34 0.0
105 Robin Manchester 38 0.0
106 Amal Cairo 31 0.0
107 Nori Osaka 37 0.0
108 John Boston 34 79.0
after drop function implementation
city py-score total-score js-score
101 Mexico City 40.0 0.0 12
102 Toronto 50.0 0.0 12
103 Prague 60.0 0.0 12
104 Shanghai 0.0 0.0 34
105 Manchester 0.0 0.0 56
106 Cairo 0.0 0.0 78
107 Osaka 0.0 0.0 90
-----------------------------------------------------------------
-----------------------------------------------------------------
import pandas as pd
import numpy as np
data = {
'name': ['Xavier', 'Ann', 'Jana', 'Yi', 'Robin', 'Amal', 'Nori'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai',
'Manchester', 'Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],
'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df = pd.DataFrame(data=data, index=row_labels)
print(df)
'''
Pandas has four accessors in total:
.loc[] accepts the labels of rows and columns and returns Series or
DataFrames.You can use it to get entire rows or columns, as well as their parts.
.iloc[] accepts the zero-based indices of rows and columns and returns Series
or DataFrames. You can use it to get entire rows or columns, or their parts.
.at[] accepts the labels of rows and columns and returns a single data value.
.iat[] accepts the zero-based indices of rows and columns and returns a single
data value.
'''
cities = df['city']
print("cities = df['city']",cities)#show output as the city columns
print("\nData manipulatation df.city \n")
print(df.city)#show output as the city columns
print(cities[102]) #show output as the city columns
print(df.loc[103]) # show output as the 103 row
print("\n\ndf.at[101,'name']\n",df.at[101,'name'])#show output as the 101 row with name columns value Xavier
print("\n\ndf.at[105,'age']\n",df.at[105,'age']) #show output as the 105 row with age columns value 38
print("\n\ndf.loc[103]\n",df.loc[103])# show out put as index value 103 complete row
print("\n\ndf.iloc[0]\n",df.iloc[0]) # show output as index 0 complete row
print("\n\ndf.loc[:, 'city']\n",df.loc[:, 'city']) #show output all row and city columns
print("\n\ndf.loc[:, 'city':]\n",df.loc[:, 'city':])#show output all row and city columns and after all columns
print("\n\ndf.loc[:, 'city']\n",df.loc[:, :'city']) # show output all rows and city and previous columns
print("\n\ndf.loc[101:, ]\n",df.loc[101:, :]) #show output 101 and all row and all columsn
print("\n\ndf.loc[:105, 'city']\n",df.loc[:105, :'city']) #show output 101 to 105 abd all columns before city and city also
print("\n\ndf.loc[:5, 'city']\n",df.loc[:5, :'city']) #show output as empty dataframe
print("\n\ndf.loc[1:, 'city']\n",df.iloc[1:6, 0:1]) #show 102 to 106 rows with name columns
print("\n\noutput df.iloc[1:6:2, 1]",df.iloc[1:6:2, 1])
print("\n\ndf.at[102, 'name']",df.at[102, 'name'])# if you want to access particular data we need at attribute
print("\n\ndf.iat[2, 0]",df.iat[2, 0])# if you want to access particular data we need at attribute
# data modification
df12=df.loc[:103, 'py-score'] = [40, 50, 60]
df13=df.loc[104:, 'py-score'] = [10,20,90,70]
print("\n\nScore after modification\n",df)
john = pd.Series(data=['John', 'Boston', 34, 79],index=df.columns, name=108)
print(john)
df111=df.append(john)
print("\n\nafter append funtion\n",df111)
df111.drop(labels=[107],inplace=True)
df['total-score'] = 0.0
df['js-score'] = [12,12,12,34,56,78,90]
print("\n\nafter adding new columns\n",df)
#print(df.insert(loc=4, column='django-score',value=[86.0, 81.0, 78.0, 88.0, 74.0, 70.0,87.0]))
#del df['name']
print("\n\nafter deleting name columns\n",df)
df = df.drop(labels=[101,103], axis=0)
print("\n\nafter drop function implementation\n",df)
# applying arithmetic operations
print("\n\nData frame Addition \n",df['py-score'] + df['js-score'])
print("\n\n Division \n",df['js-score']/2)
print("\n\n Division \n",df['js-score']*2)
df['Average']=np.average(df.iloc[:, 2:4],axis=1,weights=[0.4, 0.3,])
print("\n\n average funtion of nup \n",df)
#shorting values
df=df.sort_values(by='js-score', ascending=False)
print (df)
df=df.sort_values(by='js-score', ascending=True)
print (df)
#multiple coloum shorting
df.sort_values(by=['Average', 'py-score'], ascending=[False, False])
print(df)
#filter_ = df['django-score'] >= 80
#df[filter_]
df[(df['py-score'] >= 80) & (df['js-score'] >= 80)]
#df['django-score'].where(cond=df['django-score'] >= 80, other=0.0)
df66=df.describe()
print("fdsfdsf",df66)
df['py-score'].mean(skipna=False) #do not skip nan data
'''In the first example, .fillna(value=0) replaces the missing value with 0.0,
which you specified with value. In the second example, .fillna(method='ffill')
replaces the missing value with the value above it, which is 2.0. In the third example,
.fillna(method='bfill') uses the value below the missing value, which is 4.0.'''
df_ = pd.DataFrame({'x': [1, 2, np.nan, 4]})
df_.fillna(value=0)
df_.fillna(method='ffill')
df_.fillna(method='bfill')
df_.dropna()
#for col_label, col in df.iteritems():
# print(col_label, col, sep='\n', end='\n\n')
name city age py-score 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 cities = df['city'] 101 Mexico City 102 Toronto 103 Prague 104 Shanghai 105 Manchester 106 Cairo 107 Osaka Name: city, dtype: object Data manipulatation df.city 101 Mexico City 102 Toronto 103 Prague 104 Shanghai 105 Manchester 106 Cairo 107 Osaka Name: city, dtype: object Toronto name Jana city Prague age 33 py-score 81 Name: 103, dtype: object df.at[101,'name'] Xavier df.at[105,'age'] 38 df.loc[103] name Jana city Prague age 33 py-score 81 Name: 103, dtype: object df.iloc[0] name Xavier city Mexico City age 41 py-score 88 Name: 101, dtype: object df.loc[:, 'city'] 101 Mexico City 102 Toronto 103 Prague 104 Shanghai 105 Manchester 106 Cairo 107 Osaka Name: city, dtype: object df.loc[:, 'city':] city age py-score 101 Mexico City 41 88.0 102 Toronto 28 79.0 103 Prague 33 81.0 104 Shanghai 34 80.0 105 Manchester 38 68.0 106 Cairo 31 61.0 107 Osaka 37 84.0 df.loc[:, 'city'] name city 101 Xavier Mexico City 102 Ann Toronto 103 Jana Prague 104 Yi Shanghai 105 Robin Manchester 106 Amal Cairo 107 Nori Osaka df.loc[101:, ] name city age py-score 101 Xavier Mexico City 41 88.0 102 Ann Toronto 28 79.0 103 Jana Prague 33 81.0 104 Yi Shanghai 34 80.0 105 Robin Manchester 38 68.0 106 Amal Cairo 31 61.0 107 Nori Osaka 37 84.0 df.loc[:105, 'city'] name city 101 Xavier Mexico City 102 Ann Toronto 103 Jana Prague 104 Yi Shanghai 105 Robin Manchester df.loc[:5, 'city'] Empty DataFrame Columns: [name, city] Index: [] df.loc[1:, 'city'] name 102 Ann 103 Jana 104 Yi 105 Robin 106 Amal output df.iloc[1:6:2, 1] 102 Toronto 104 Shanghai 106 Cairo Name: city, dtype: object df.at[102, 'name'] Ann df.iat[2, 0] Jana Score after modification name city age py-score 101 Xavier Mexico City 41 40.0 102 Ann Toronto 28 50.0 103 Jana Prague 33 60.0 104 Yi Shanghai 34 10.0 105 Robin Manchester 38 20.0 106 Amal Cairo 31 90.0 107 Nori Osaka 37 70.0 name John city Boston age 34 py-score 79 Name: 108, dtype: object after append funtion name city age py-score 101 Xavier Mexico City 41 40.0 102 Ann Toronto 28 50.0 103 Jana Prague 33 60.0 104 Yi Shanghai 34 10.0 105 Robin Manchester 38 20.0 106 Amal Cairo 31 90.0 107 Nori Osaka 37 70.0 108 John Boston 34 79.0
-----------------------------------------------------------------------------------------------------------------------
0 Comments