Reputation: 1572
Suppose I have a pandas dataframe with columns 0, 1 and 'Future Connections'. How do I set columns 0 and 1 as one tuple index:
For example this data frame:
0 1 Future Connection
6 840 0.0
4 197 1.0
620 979 0.0
would result to:
0 Future Connection
(6, 840) 0.0
(4, 197) 1.0
(620, 979) 0.0
Upvotes: 3
Views: 4099
Reputation: 164693
How do I set columns 0 and 1 as one tuple index:
"Tuple index" as a concept does not exist in Pandas. You can have an object
dtype index containing tuples, but this isn't recommended. The best option is to use a MultiIndex
, which stores underlying values efficiently via NumPy arrays. Indeed, Pandas facilitates this with set_index
:
df = df.set_index([0, 1])
print(df)
# Future Connection
# 0 1
# 6 840 0.0
# 4 197 1.0
# 620 979 0.0
print(df.index)
# MultiIndex(levels=[[4, 6, 620], [197, 840, 979]],
# labels=[[1, 0, 2], [1, 0, 2]],
# names=[0, 1])
print(df.index.values)
# [(6, 840) (4, 197) (620, 979)]
Upvotes: 5
Reputation: 862771
Use list comprehension with DataFrame.pop
for extract column 0, 1
:
print (df.columns)
Index([0, 1, 'Future Connection'], dtype='object')
df.index = [x for x in zip(df.pop(0), df.pop(1))]
print (df)
Future Connection
(6, 840) 0.0
(4, 197) 1.0
(620, 979) 0.0
Upvotes: 4