xxxxxxxxxx
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['John', 'Smith', 'Alice', 'Bob'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'Chicago', 'Los Angeles', 'Boston']}
df = pd.DataFrame(data)
# Access specific rows and columns using iloc
# Syntax: iloc[row_index, column_index]
# Access the value in the first row, second column
value = df.iloc[0, 1]
print(value) # Output: 25
# Access a subset of rows and columns using iloc
subset = df.iloc[1:3, 0:2]
print(subset)
# Output:
# Name Age
# 1 Smith 30
# 2 Alice 35
xxxxxxxxxx
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
'a': 100, 'b': 200, 'c': 300, 'd': 400}, {
'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }] {
>>> df = pd.DataFrame(mydict)
>>> df
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
>>>type(df.iloc[0])
<class 'pandas.core.series.Series'>
>>>df.iloc[0]
a 1
b 2
c 3
d 4
Name: 0, dtype: int64
xxxxxxxxxx
df.iloc[[0]]
a b c d
0 1 2 3 4
>>> type(df.iloc[[0]])
<class 'pandas.core.frame.DataFrame'>
xxxxxxxxxx
df.iloc[:3]
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000