📊 Unit 1 – DataFrame · Lesson 09
DataFrame: बनाना और Attributes
DataFrame क्या है?
DataFrame two-dimensional labelled table है – Excel की sheet या MySQL की table जैसा। इसमें row index और column labels होते हैं, और हर column का data type अलग हो सकता है।
| Name | Maths | IP | |
|---|---|---|---|
| 0 | Asha | 78 | 88 |
| 1 | Ravi | 92 | 81 |
| 2 | Neha | 65 | 95 |
| 3 | Om | 88 | 74 |
ख़ाली DataFrame
Python
import pandas as pd
df = pd.DataFrame() # an empty DataFrame
print(df)
print('empty:', df.empty)Empty DataFrame
Columns: []
Index: []
empty: True
Lists की dictionary से
Python
import pandas as pd
result = {'Name': ['Asha', 'Ravi', 'Neha', 'Om'],
'Maths': [78, 92, 65, 88],
'IP': [88, 81, 95, 74]}
df = pd.DataFrame(result) # keys -> columns
print(df) Name Maths IP
0 Asha 78 88
1 Ravi 92 81
2 Neha 65 95
3 Om 88 74
Dictionaries की list से
Python
import pandas as pd
rows = [{'City': 'Delhi', 'Temp': 31}, {'City': 'Pune', 'Temp': 27},
{'City': 'Leh', 'Temp': 12, 'Snow': 'Yes'}]
df = pd.DataFrame(rows) # each dict is one ROW
print(df) City Temp Snow
0 Delhi 31 NaN
1 Pune 27 NaN
2 Leh 12 Yes
Lists की list से
Python
import pandas as pd data = [[101, 'Pen', 10], [102, 'Pencil', 5], [103, 'Eraser', 3]] df = pd.DataFrame(data, columns=['Code', 'Item', 'Price'], index=['a', 'b', 'c']) print(df)
Code Item Price
a 101 Pen 10
b 102 Pencil 5
c 103 Eraser 3
Series से
Python
import pandas as pd
maths = pd.Series([78, 92, 65], index=['Asha', 'Ravi', 'Neha'])
ip = pd.Series([88, 81, 95], index=['Asha', 'Ravi', 'Neha'])
df = pd.DataFrame({'Maths': maths, 'IP': ip}) # dictionary of Series
print(df) Maths IP
Asha 78 88
Ravi 92 81
Neha 65 95
NumPy array से
Python
import pandas as pd import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) df = pd.DataFrame(arr, columns=['A', 'B', 'C']) print(df)
A B C
0 1 2 3
1 4 5 6
Attributes
| Attribute | Gives |
|---|---|
| index | Row labels |
| columns | Column labels |
| dtypes | Data type of each column |
| values | Data as a NumPy array |
| shape | (rows, columns) |
| size | rows × columns |
| ndim | Number of dimensions (2) |
| T | Transpose |
| empty | True if no data |
Python
import pandas as pd
df = pd.DataFrame({'Maths': [78, 92, 65, 88], 'IP': [88, 81, 95, 74],
'Grade': ['B', 'A', 'B', 'A']},
index=['Asha', 'Ravi', 'Neha', 'Om'])
print('index :', df.index.tolist())
print('columns:', df.columns.tolist())
print('shape :', df.shape, '| size:', df.size, '| ndim:', df.ndim)
print(df.dtypes)
print(df.T) # transpose: rows <-> columnsindex : ['Asha', 'Ravi', 'Neha', 'Om']
columns: ['Maths', 'IP', 'Grade']
shape : (4, 3) | size: 12 | ndim: 2
Maths int64
IP int64
Grade object
dtype: object
Asha Ravi Neha Om
Maths 78 92 65 88
IP 88 81 95 74
Grade B A B A
Python
import pandas as pd
df = pd.DataFrame({'Maths': [78, 92, 65, 88], 'IP': [88, 81, 95, 74]},
index=['Asha', 'Ravi', 'Neha', 'Om'])
print(df.values)
print(df.head(2))
print(df.tail(1))[[78 88]
[92 81]
[65 95]
[88 74]]
Maths IP
Asha 78 88
Ravi 92 81
Maths IP
Om 88 74
सारांश
📘
dict of listskeys → columns📗
list of dictsहर dict → एक row📙
list of listsहर अंदर की list → एक row; columns= दें📕
dict of SeriesSeries index → row indexअक्सर पूछे जाने वाले प्रश्न (FAQ)
Lists की dictionary में keys rows बनती हैं या columns?
Columns। हर key column का नाम है और उसकी list column का data।
shape क्या लौटाता है?
Tuple (rows, columns)। 4 rows और 3 columns वाले DataFrame का shape (4, 3) और size 12 है।
List की किसी एक dictionary में extra key हो तो क्या होता है?
उस key का नया column बनता है और बाकी rows में उसमें NaN आता है।
