# Libraries
import pandas as pd
# Dataframe created from a dictionary
data1 = {
"col1":[1,2,3,4,5,6,7,8,9,10],
"col2":[11,12,13,14,15,16,17,18,19,20]
}
df = pd.DataFrame(data = data1)
print(df)
col1 col2
0 1 11
1 2 12
2 3 13
3 4 14
4 5 15
5 6 16
6 7 17
7 8 18
8 9 19
9 10 20
# Dataframe created from a list of lists
data2 = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
df = pd.DataFrame(data = data2, columns=("col1","col2","col3","col4","col5"))
print(df)
col1 col2 col3 col4 col5
0 1 2 3 4 5
1 6 7 8 9 10
2 11 12 13 14 15
3 16 17 18 19 20
# working from the created dictionary
df = pd.DataFrame(data = data1,
index=["row1","row2","row3","row4","row5","row6","row7","row8","row9","row10"])
print(df)
col1 col2
row1 1 11
row2 2 12
row3 3 13
row4 4 14
row5 5 15
row6 6 16
row7 7 17
row8 8 18
row9 9 19
row10 10 20
data4 = [{"col1": 1, "col2": 2},
{"col1": 2, "col2": 4},
{"col1": 3, "col2": 6},
{"col1": 4, "col2": 8},
{"col1": 5, "col2": 10}]
df = pd.DataFrame(data = data4)
print(df)
col1 col2
0 1 2
1 2 4
2 3 6
3 4 8
4 5 10
# original lists
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# transformed list with zip function
data5 = list(zip(x, y))
print(data5)
# the dataset created
df = pd.DataFrame(data5, columns = ["x", "y"])
print(df)
[(1, 2), (2, 4), (3, 6), (4, 8), (5, 10)]
x y
0 1 2
1 2 4
2 3 6
3 4 8
4 5 10
# The 'orient' parameter changes the orientation of columns by rows
data6 = {"key1": [1, 4, 7],
"key2": [2, 5, 8],
"key3": [3, 6, 9]}
df = pd.DataFrame.from_dict(data6, orient = "index", columns = ["A", "B", "C"])
print(df)
A B C
key1 1 4 7
key2 2 5 8
key3 3 6 9
# Dataframe created from a dictionary
data3 = {
"col1":[1,2,3,4,5,6,7,8,9,10],
"col2":[11,12,13,14,15,16,17,18,19,20],
"col3":[21,22,23,24,25,26,27,28,29,30],
"col4":[31,32,33,34,35,36,37,38,39,40],
"col5":[41,42,43,44,45,46,47,48,49,50],
"col6":[51,52,53,54,55,56,57,58,59,60]
}
df = pd.DataFrame(data = data3)
print(df)
col1 col2 col3 col4 col5 col6
0 1 11 21 31 41 51
1 2 12 22 32 42 52
2 3 13 23 33 43 53
3 4 14 24 34 44 54
4 5 15 25 35 45 55
5 6 16 26 36 46 56
6 7 17 27 37 47 57
7 8 18 28 38 48 58
8 9 19 29 39 49 59
9 10 20 30 40 50 60
df1 = pd.DataFrame(data = data3, columns=["col1","col4","col6"])
print(df1)
col1 col4 col6
0 1 31 51
1 2 32 52
2 3 33 53
3 4 34 54
4 5 35 55
5 6 36 56
6 7 37 57
7 8 38 58
8 9 39 59
9 10 40 60
#df["col2"] <-- 1 column
df[["col2", "col3"]] #<-- more than one column
col2int64
11 - 20
col3int64
21 - 30
0
11
21
1
12
22
2
13
23
3
14
24
4
15
25
5
16
26
6
17
27
7
18
28
8
19
29
9
20
30
df[df.columns[1]] #<-- 1 column
#df[df.columns[[1,2]]] #<-- more than one column
#df[df.columns[0:4]] #<-- from column 0 to column 3
# all rows and ...
df.loc[:, "col2"] #<-- 1 column
#df.loc[:, ["col2","col3"]] #<-- more than one column
#df.loc[:, "col2":"col5"] #<-- from column "col2" to column "col5"
# all rows and ...
#df.iloc[:, 1] #<-- 1 column
#df.iloc[:, [0,1]] #<-- more than one column
df.iloc[:, 0:3] #<-- from column 0 to column 2
col1int64
1 - 10
col2int64
11 - 20
0
1
11
1
2
12
2
3
13
3
4
14
4
5
15
5
6
16
6
7
17
7
8
18
8
9
19
9
10
20