DataType of Pandas Multiindex [duplicate]

2024/11/16 7:34:58
import pandas as pd
index = pd.MultiIndex.from_tuples([(1,2, None), (1,2,3)])
print(index.get_level_values(2))

gives

Int64Index([nan, 3.0], dtype='float64')

However, I would like to get

Int64Index([nan, 3], dtype='object')

That is, Pandas should not automatically change the data type. How can I do this?

Answer

I think you can convert level values to string by astype, str.split it by . and get first value. Then create new MultiIndex:

#change multiindex
new = zip(index.get_level_values(0),index.get_level_values(1),index.get_level_values(2).astype(str).str.split('.').str.get(0))new_index = pd.MultiIndex.from_tuples(new)
print (new_index)
MultiIndex(levels=[[1], [2], [u'3', u'nan']],labels=[[0, 0], [0, 0], [1, 0]])print(new_index.get_level_values(2))
Index([u'nan', u'3'], dtype='object')
https://en.xdnf.cn/q/118950.html

Related Q&A

Is there a way to see the internal representation of float?

In python tutorial for floating point, In base 2, 1/10 is the infinitely repeating fraction0.0001100110011001100110011001100110011001100110011...How do I get python interpreter to print this intenal re…

Create function from try-except

Based on the example provided in this answer, how can I create a function from:from collections import Counter s = [0, 0, 2, 1, 1, 0, 0, 0] try:print(next(t[0] for t in Counter(s).most_common(2) if t[…

Bad request error flask with POST request

I have the following route that is getting values from a submitted form@app.route(/authenticate, methods=["POST"]) def authenticate():username = request.form[username]print(username, file = s…

How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

Im wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity…

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…