Reputation: 18547
Given
import pandas as pd
df = pd.DataFrame({'lowercase':['a','b','c','d']},
index=[0,1,3,4])
# print(df)
print(df['lowercase'].iat[0])
I can get 'a'
, but I want to get the first index (which is 0
) and 'a'
return as two values, how should I do?
Upvotes: 0
Views: 215
Reputation: 20689
You can use df.itertuples
which returns a map object.
def get_item(idx):
it = df.itertuples()
while idx:
next(it)
idx = idx-1
return next(it)
a,b = get_item(0)
# a = 0 , b = 'a'
c,d = get _item(1)
# c = 1, d = 'b'
You can even get a slice using islice
from itertools.
def get_item(start,end=None):
it = df.itertuples()
if end is None:
while idx:
next(it)
idx = idx-1
return next(it)
return list(map(tuple,islice(it,start,end)))
get_item(1, 4)
# [(1, 'b'), (3, 'c'), (4, 'd')]
If you only want first value
a,b = next(df.itertuples())
# a = 0, b = 'a'
Upvotes: 1