set index in datarame
df = df.set_index('col')
add an index column pandas
import numpy as np
import pandas as pd
df = pd.DataFrame({'month': [2, 5, 8, 10],
'year': [2017, 2019, 2018, 2019],
'sale': [60, 45, 90, 36]})
df.set_index('month')
how to index a set in python
"""
You can't index a set in python.
The concept of sets in Python is inspired in Mathmatics. If you are interested
in knowing if any given item belongs in a set, it shouldn't matter "where" in
the set that item is located. With that being said, if you still want to
accomplish this, this code should work, and, simultaneously, show why it's a
bad idea
"""
def get_set_element_from_index (input_set : set, target_index : int):
if target_index < 0:
target_index = len(input_set) + target_index
index = 0
for element in input_set:
if index == target_index:
return element
index += 1
raise IndexError ('set index out of range')
my_set = {'one','two','three','four','five','six'}
print (my_set) # unpredictable output. Changes everytime you run the script
# let's say the previous print nails the order of the set (same as declared)
print(get_set_element_from_index(my_set, 0)) # prints 'one'
print(get_set_element_from_index(my_set, 3)) # prints 'four'
print(get_set_element_from_index(my_set, -1)) # prints 'six'
print(get_set_element_from_index(my_set, 6)) # raises IndexError
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us