Python | Панды Index.to_frame ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas Index.to_frame() function create a dataFrame from the given index with a column containing the Index. By default, the original Index is reused in the new dataframe. To reinforce a new index for the newly created dataframe, we set the index parameter of the function to be false.
Syntax: Index.to_frame(index=True)
Parameters :
index : Set the index of the returned DataFrame as the original Index.Returns : DataFrame containing the original Index data.
Example #1: Use Index.to_frame() function to convert the index into a dataframe.
# importing pandas as pdimport pandas as pd # Creating the indexidx = pd.Index(["Alice", "Bob", "Rachel", "Tyler", "Louis"], name ="Winners") # Print the Indexidx |
Выход :
Let’s convert the index into a dataframe.
# convert the index into a dataframeidx.to_frame() |
Output :
The function has converted the index into a dataframe. By default the function has created the index of the dataframe using the values of the original Index.
Example #2: Use Index.to_frame() function to convert the index into a dataframe such that the dataframe created uses new index value.
# importing pandas as pdimport pandas as pd # Creating the indexidx = pd.Index([22, 54, 85, 45, 69, 33]) # Print the Indexidx |
Выход :
Let’s convert the index into a dataframe.
# convert the index into a dataframeidx.to_frame(index = False) |
Выход :
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.