0% found this document useful (0 votes)
27 views

pandas (1)

For more information about data science
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

pandas (1)

For more information about data science
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

pandas

November 17, 2024

[22]: import pandas as pd


a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)

0 1
1 7
2 2
dtype: int64

[24]: import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a, index = ["x", "y", "z"])

print(myvar)
print(myvar["y"])

x 1
y 7
z 2
dtype: int64
7

[28]: import pandas as pd

d = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)

print(df)
print(type(d))
print(df.loc[0])

1
calories duration
0 420 50
1 380 40
2 390 45
<class 'dict'>
calories 420
duration 50
Name: 0, dtype: int64

[34]: import pandas as pd

d = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)
print(df.loc[[0, 1, 2]])

calories duration
0 420 50
1 380 40
2 390 45

[1]: import pandas as pd

data = [10, 20, 30, 40]


series = pd.Series(data, index=['a', 'b', 'c', 'd'])
print(series)

a 10
b 20
c 30
d 40
dtype: int64

[2]: import pandas as pd

data = [10, 20, 30, 40]


series = pd.Series(data)
print(series)

0 10
1 20
2 30
3 40
dtype: int64

2
[1]: import pandas as pd

df = pd.read_csv(r'C:\Users\chido\Downloads\laptops.csv')

print(df)

Laptop Status Brand \


0 ASUS ExpertBook B1 B1502CBA-EJ0436X Intel Core… New Asus
1 Alurin Go Start Intel Celeron N4020/8GB/256GB … New Alurin
2 ASUS ExpertBook B1 B1502CBA-EJ0424X Intel Core… New Asus
3 MSI Katana GF66 12UC-082XES Intel Core i7-1270… New MSI
4 HP 15S-FQ5085NS Intel Core i5-1235U/16GB/512GB… New HP
… … … …
2155 Razer Blade 17 FHD 360Hz Intel Core i7-11800H/… Refurbished Razer
2156 Razer Blade 17 FHD 360Hz Intel Core i7-11800H/… Refurbished Razer
2157 Razer Blade 17 FHD 360Hz Intel Core i7-11800H/… Refurbished Razer
2158 Razer Book 13 Intel Evo Core i7-1165G7/16GB/1T… Refurbished Razer
2159 Razer Book FHD+ Intel Evo Core i7-1165G7/16GB/… Refurbished Razer

Model CPU RAM Storage Storage type GPU \


0 ExpertBook Intel Core i5 8 512 SSD NaN
1 Go Intel Celeron 8 256 SSD NaN
2 ExpertBook Intel Core i3 8 256 SSD NaN
3 Katana Intel Core i7 16 1000 SSD RTX 3050
4 15S Intel Core i5 16 512 SSD NaN
… … … … … … …
2155 Blade Intel Core i7 16 1000 SSD RTX 3060
2156 Blade Intel Core i7 16 1000 SSD RTX 3070
2157 Blade Intel Core i7 32 1000 SSD RTX 3080
2158 Book Intel Evo Core i7 16 1000 SSD NaN
2159 Book Intel Evo Core i7 16 256 SSD NaN

Screen Touch Final Price


0 15.6 No 1009.00
1 15.6 No 299.00
2 15.6 No 789.00
3 15.6 No 1199.00
4 15.6 No 669.01
… … … …
2155 17.3 No 2699.99
2156 17.3 No 2899.99
2157 17.3 No 3399.99
2158 13.4 Yes 1899.99
2159 13.4 Yes 1699.99

[2160 rows x 12 columns]

3
[13]: import pandas as pd

data = {
"Duration":{
"0":60,
"1":60,
"2":60,
"3":45,
"4":45,
"5":60
},
"Pulse":{
"0":110,
"1":117,
"2":103,
"3":109,
"4":117,
"5":102
},
"Maxpulse":{
"0":130,
"1":145,
"2":135,
"3":175,
"4":148,
"5":127
},
"Calories":{
"0":409.1,
"1":479.0,
"2":340.0,
"3":282.4,
"4":406.0,
"5":300.5
}
}

df = pd.DataFrame(data)

print(df)

Duration Pulse Maxpulse Calories


0 60 110 130 409.1
1 60 117 145 479.0
2 60 103 135 340.0
3 45 109 175 282.4
4 45 117 148 406.0

4
5 60 102 127 300.5

[14]: import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)

print(df)

calories duration
0 420 50
1 380 40
2 390 45

[15]: import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

df = pd.DataFrame(data, index = ["day1", "day2", "day3"])

print(df)

calories duration
day1 420 50
day2 380 40
day3 390 45

[16]: import pandas as pd

# List of lists
data = [
['Alice', 24, 'New York'],
['Bob', 27, 'Los Angeles'],
['Charlie', 22, 'Chicago']
]

# Creating DataFrame with specified column names


df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
print(df)

5
Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago

[17]: # List of dictionaries


data = [
{'Name': 'Alice', 'Age': 24, 'City': 'New York'},
{'Name': 'Bob', 'Age': 27, 'City': 'Los Angeles'},
{'Name': 'Charlie', 'Age': 22, 'City': 'Chicago'}
]

# Creating DataFrame
df = pd.DataFrame(data)
print(df)

Name Age City


0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago

[18]: # Dictionary of lists


data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22],
'City': ['New York', 'Los Angeles', 'Chicago']
}

# Creating DataFrame
df = pd.DataFrame(data)
print(df)

Name Age City


0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago

[ ]:

You might also like