Using PYTHON Pandas DataFrame
A data frame is a two-dimensional data structure, that is, the data is aligned in rows and columns in a table. DataFrame Features:
- Potentially columns are of different types
- Size can be changed
- Consists of rows and columns
- Arithmetic operations can be done in rows and columns
DataFrame parameters:
- data: The data takes various forms such as ndarray, serial, map, list, dictation, constants and other data such as DataFrame.
- index: index is used optionally. Sets row
- columns: Optionally used for column processing.
- dtype: The data type of each column.
- copy: False the default value. It is used to be copied using the command.
Creating a DataFrame
A pandas DataFrame can be created using several entries:
- List
- dict
- Series
- Numpy ndarrays
Another DataFrame In the next sections of this section, we will see how to create a data frame using these entries.
Creating an Empty DataFrame
Empty DataFrame Columns: [] Index: []
Creating a DataFrame with a List
DataFrame can be created using a single list or array list.
Example 1:
0
0 1
1 2
2 3
3 4
4 5
Example 2:
Name Age
0 Furkan 22
1 Ayse 25
2 Osman 27
Example 3:
Name Age
0 Furkan 22.0
1 Ayse 25.0
2 Osman 27.0
Creating a DataFrame with Dict
All ndarray should be the same length. The length of the string must be equal to the length of the other string.
Example 1:
Name Age
0 Furkan 22
1 Ayse 25
2 Osman 27
Example 2: Let’s create an indexed Data Dataframe using arrays.
Name Age
1.Person Furkan 22
2.Person Ayse 25
3.Person Osman 27
Example 3: We can write the dict structure in a shorter way by using it directly.
Name Age
0 Furkan 22
1 Ayse 25
2 Osman 27
Example 4:
Name Age
1.Person Furkan 22
2.Person Ayse 25
3.Person Osman 27
Example 5:
name profession
a Ayse Doctor
b Fatma Engineer
c Hayriye Architect
d Betul NaN
Adding a New Column with Pandas
We apply the following structure to add a new column to the DataFrame data.
age column added name profession age
a Ayse Doctor 24
b Fatma Engineer 26
c Hayriye Architect 32hometown column added name profession age hometown
a Ayse Doctor 24 Ankara
b Fatma Engineer 26 Sinop
c Hayriye Architect 32 Amasya
Deleting Columns with Pandas
Here, we will delete the 2 columns we created above.
age column is being deletedname profession hometown
a Ayse Doctor Ankara
b Fatma Engineer Sinop
c Hayriye Architect Amasyahometown column is being deleted name profession
a Ayse Doctor
b Fatma Engineer
c Hayriye Architect
Line Selection with Pandas
There are two ways to select lines in the Pandas library. One of them reaches the line directly with the name of the label, and the other with the position found. I will show the next transactions using the DataFrame below.
letter value
first a 1
second b 2
third c 3
Inserting Rows with Pandas
The append function is used to add rows to the Pandas DataFrame structure.
letter value
first a 1
second b 2
third c 3
fourth d 4
Deleting Rows with Pandas
Use the drop tag to delete or drop rows from a data frame. If the tag is duplicated, multiple lines are deleted.
letter value
first a 1
second b 2
third c 3