Data Frame
Data Frame
Pandas Dataframe
The simple datastructure pandas.DataFrame is described in this article. It includes
the related information about the creation, index, addition and deletion. The text is
very detailed.
In short: it’s a two-dimensional data structure (like table) with rows and columns.
Related course: Data Analysis with Python Pandas
Create DataFrame
What is a Pandas DataFrame
Pandas is a data manipulation module. DataFrame let you store tabular data in
Python.
Python.
The DataFrame lets you easily store and manipulate tabular data like rows and
columns.
A dataframe can be created from a list (see below), or a dictionary or numpy array
(see bottom).
Create DataFrame from list
You can turn a single list into a pandas dataframe:
import pandas as pd
pd
data = [1
[1,2,3]
df = pd.DataFrame(data)
pd.DataFrame(data)
0 1
1 2
2 3
>>>
>>>
Before the contents, you’ll see every element has an index (0,1,2).
(0,1,2).
This works for tables (n-dimensional arrays) too:
import pandas as pd
pd
data = [['Axel'
[['Axel',
,32
32],
], ['Alice'
['Alice',
, 26
26],
], ['Alex'
['Alex',
, 45
45]]
]]
df = pd.DataFrame(data,columns=['Name'
pd.DataFrame(data,columns=['Name',
,'Age'
'Age'])
])
This outputs:
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
>>>
>>>
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
0 Axel
Axel
1 Alice
Alice
2 Alex
Alex
object
>>> df[
df['Age'
'Age']
]
0 32
32
1 26
26
2 45
45
int64
>>>
>>>
Column Addition
You can add a column to a dataframe. So this:
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
Becomes this:
>>> df
df
Example
0 Axel 32 1
1 Alice 26 2
2 Alex 45 3
>>>
>>>
>>> df = pd.DataFrame(data,columns=['Name'
pd.DataFrame(data,columns=['Name',
,'Age'
'Age'])
])
>>>
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
column
>>> c = pd.DataFrame([1
pd.DataFrame([1,2,3], columns=['Example'
columns=['Example'])
])
Step 3: Set the column name of your dataframe to that of the newly created one:
one:
>>> df['Example'
df['Example']
] = c['Example'
c['Example']
]
>>> df
df
Example
0 Axel 32 1
1 Alice 26 2
2 Alex 45 3
>>>
>>>
Column deletion
To delete a column, you can use the keyword del
del..
df
Example
0 Axel 32 1
1 Alice 26 2
2 Alex 45 3
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
>>>
>>>
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
>>>
>>> df.loc[
df.loc[00]
Name Axel
Axel
Age 32
32
object
>>>
>>> df.loc[
df.loc[22]
Name Alex
Alex
Age 45
45
object
>>>
>>>
Name Axel
Axel
Age 32
32
object
>>>
>>>
Append row
You can append a row by calling the .append() method on the dataframe.
dataframe.
First create a new dataframe:
>>> user = pd.DataFrame([['Vivian'
pd.DataFrame([['Vivian',
,33
33]],
]], columns= ['Name'
['Name',
,'Age'
'Age'])
])
df.append(user)
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
0 Vivian 33
33
>>>
>>>
Delete row
To delete a row, you can use the method .drop(index)
.drop(index)..
Start by creating a frame:
frame:
>>> data = [['Axel'
[['Axel',
,32
32],
], ['Alice'
['Alice',
, 26
26],
], ['Alex'
['Alex',
, 45
45]]
]]
>>> df = pd.DataFrame(data,columns=[
pd.DataFrame(data,columns=['Name'
'Name',
,'Age'
'Age'])
])
>>> df
df
Name Age
Age
0 Axel 32
32
1 Alice 26
26
2 Alex 45
45
>>> df
df
Name Age
Age
1 Alice 26
26
2 Alex 45
45
>>>
>>>
DataFrame creation
Create DataFrame from dictionary
If you have a dictionary, you can turn it into a dataframe.
>>> import pandas as pd
pd
aa>>> d = {'one'
{'one':[
:[1
1,2,3], 'two'
'two':[
:[2
2,3,4], 'three'
'three':[
:[3
3,4,5] }
>>> df = pd.DataFrame(d)
pd.DataFrame(d)
>>> df
df
three
0 1 2 3
1 2 3 4
2 3 4 5
>>>
>>>
The keys in the dictionary are columns in the DataFrame, but there is no value for
the index, so you need to set it yourself, and no default is to count from zero.
>>> df = pd.DataFrame(d, index=['first'
index=['first',
,'second'
'second',
,'third'
'third'])
])
>>> df
df
three
first 1 2 3
second 2 3 4
third 3 4 5
>>>
>>>
>>> ar = np.array([[1
np.array([[1,2,3],[
],[4
4,5,6],[
],[6
6,7,8]])
]])
>>> ar
ar
array([[1
array([[ 1, 2, 3],
],
[4
[4, 5, 6],
],
[6
[6, 7, 8]])
]])
pd.DataFrame(ar)
>>> df
df
0 1 2
0 1 2 3
1 4 5 6
2 6 7 8
>>>
>>>
Three
A 1 2 3
B 4 5 6
C 6 7 8
>>>
>>>
dataframe.
Using the dataframe above:
>>> df2 = df[['One'
df[['One',
,'Two'
'Two']].copy()
]].copy()
>>> df2
df2
One Two
Two
A 1 2
B 4 5
C 6 7
>>>
>>>
import pandas as pd
pd
cats = pd.read_csv('cats.csv'
pd.read_csv('cats.csv')
)
print(cats)
print(cats)
Back Next
Pandas Series Read CSV with Pandas
Cookie policy |
Privacy policy |
Terms of use |
© 2021 https://pythonbasics.org