-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathData Manupulation Using pandas.txt
67 lines (44 loc) · 1.71 KB
/
Data Manupulation Using pandas.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Importing Libraries
To begin with data manipulation, you will need to import the necessary libraries. The most commonly used library for this purpose is pandas. You can import pandas using the following code:
import pandas as pd
Loading Data
Before you can start manipulating data, you will need to load the data into a pandas DataFrame. You can do this using the pandas read_csv function, or by loading data from another source such as a database.
# Load data from a CSV file
df = pd.read_csv('data.csv')
Selecting Data
You can select data from a DataFrame using the iloc and loc indexers.
# Select a single value
df.iloc[row, col]
df.loc[row, col]
# Select a row or column
df.iloc[row]
df.loc[row]
df.iloc[:, col]
df.loc[:, col]
# Select a range of rows or columns
df.iloc[start:end]
df.loc[start:end]
df.iloc[:, start:end]
df.loc[:, start:end]
Filtering Data
You can filter data from a DataFrame using boolean indexing.
# Filter rows based on a condition
df[df['column'] > value]
# Combine multiple conditions using & and |
df[(df['column_1'] > value_1) & (df['column_2'] < value_2)]
df[(df['column_1'] > value_1) | (df['column_2'] < value_2)]
Sorting Data
You can sort data in a DataFrame using the sort_values method.
# Sorting Data
# Sort data by a single column
df.sort_values('column')
# Sort data by multiple columns
df.sort_values(['column_1', 'column_2'])
# Sort data in descending order
df.sort_values('column', ascending=False)
# Stable sort data by a single column
df.sort_values('column', kind='mergesort')
# Stable sort data by multiple columns
df.sort_values(['column_1', 'column_2'], kind='mergesort')
# Sort data using the index
df.sort_index()