# 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)
# 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)
# working from the created dictionary
df = pd.DataFrame(data = data1,
index=["row1","row2","row3","row4","row5","row6","row7","row8","row9","row10"])
print(df)
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)
# 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)
# 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)
# 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)
df1 = pd.DataFrame(data = data3, columns=["col1","col4","col6"])
print(df1)
#df["col2"] <-- 1 column
df[["col2", "col3"]] #<-- more than one column
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