Schemas
Used In Pandas Dataframes
In this tutorial, we are going to learn about Schemas Used In Pandas Dataframes.In last tutorial, my friends ask me to give us a proper
description on schemas used in pandas dataframes.
But first
we have to learn about dataframes.So let’s start our tutorial without any delay
:-
Introduction
A Data frame is a two-dimensional data structure,
where data is aligned in a tabular fashion in rows and columns.
Features
of Pandas DataFrame
·
Columns are of
different types in datframes
·
Sizes are
Mutable by nature
·
Labeled axes
(rows and columns) in tabular form
·
We could
Perform the Arithmetic operations on rows and columns
Create
Your Own DataFrame
A basic DataFrame, which can be created Your Own
Dataframe.
import pandas as pd
df = pd.DataFrame()
print (df)
Create a
DataFrame from Lists
The DataFrame can be created using a single list or a
list of lists.some example is given :-
import pandas as pd
data = [33,22,45,6,7,8,9]
df = pd.DataFrame(data)
print df
-----------------OR---------------------
import pandas as pd
data =
[['pawan',103],['rahul',17],['pal',1356]]
df =
pd.DataFrame(data,columns=['Name','id'])
print df
Create a
DataFrame from Dictionaries
List of Dictionaries can be passed as input data to
create a DataFrame. The dictionary keys are by default taken as column names.
The following example shows how to create a DataFrame
by passing a list of dictionaries.
import pandas as pd
data = [{'a': 1, 'b':
2},{'a': 3, 'b': 4, 'c': 5}]
df = pd.DataFrame(data)
print df
Column Selection
We will understand this by selecting a column from the
DataFrame.
Example
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']
Multiple
schemas Used In Pandas Dataframe
import
pandas as pd
import
numpy as np
df
= pd.DataFrame({
'col_1':
[0, 1, 2, 3],
'col_2':
[4, 5, 6, 7]
})
Combine schemas In Pandas Dataframes
df
= df.join(pd.DataFrame(
{
'column_new_1':
np.nan,
'column_new_2':
'humans',
'column_new_3':
3
},
index=df.index
))
Column
Addition
We will understand this by adding a new column to an
existing data frame.
Example
import pandas as pd
data = {'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(data)
Addition
of Rows
Add new rows to a DataFrame using the append function.
This function will append the rows at the end.
Example
import pandas as pd
df = pd.DataFrame([[1, 2],
[3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5,
6], [7, 8]], columns = ['a','b'])
df = df.append(df2)
print df
Deletion
of Rows
Use index label to delete or drop rows from a
DataFrame. If label is duplicated, then multiple rows will be dropped.
If you observe, in the above example, the labels are
duplicate. Let us drop a label and will see how many rows will get dropped.
Example
import pandas as pd
df = pd.DataFrame([[1, 2],
[3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5,
6], [7, 8]], columns = ['a','b'])
df = df.append(df2)
---------OR------------
df = df.drop(0)
print (df)
This is the the end of our tutorial guys.
If you liked this post, then please distribute it among your friend circle or on the social media platforms you use.
Thank you Guys and Good Bye...
Thank you Guys and Good Bye...
Keep learning,
0 Comments