Indexes and Columns
You can change DataFrame' indexes likewise Series' indexes (by assigning necessary list to the .index
attribute of DataFrame).
12345678# Importing library import pandas as pd # Creating DataFrame df = pd.DataFrame([[1, 2], [3, 4], [5, 6]]) # Set indexes df.index = ['first', 'second', 'third'] print(df)
You can set indexes when defining the DataFrame by specifying the index
parameter. For instance,
123456# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], index = ['first', 'second', 'third']) print(df)
Also there are two ways to set the columns names: by assigning list of names to the columns
attribute, or specifying the columns
parameter.
12345678910# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns = ['col1', 'col2'], index = ['first', 'second', 'third']) print(df) # Changing columns' names df.columns = ['Column1', 'Column2'] print(df)
If you need to change columns' names or indexes of existing dataframe, use the approach with reassigning (
.index
,.columns
attributes).
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Pregunte me preguntas sobre este tema
Resumir este capítulo
Mostrar ejemplos del mundo real
Awesome!
Completion rate improved to 3.33
Indexes and Columns
Desliza para mostrar el menú
You can change DataFrame' indexes likewise Series' indexes (by assigning necessary list to the .index
attribute of DataFrame).
12345678# Importing library import pandas as pd # Creating DataFrame df = pd.DataFrame([[1, 2], [3, 4], [5, 6]]) # Set indexes df.index = ['first', 'second', 'third'] print(df)
You can set indexes when defining the DataFrame by specifying the index
parameter. For instance,
123456# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], index = ['first', 'second', 'third']) print(df)
Also there are two ways to set the columns names: by assigning list of names to the columns
attribute, or specifying the columns
parameter.
12345678910# Importing library import pandas as pd # Creating DataFrame with indexes df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], columns = ['col1', 'col2'], index = ['first', 'second', 'third']) print(df) # Changing columns' names df.columns = ['Column1', 'Column2'] print(df)
If you need to change columns' names or indexes of existing dataframe, use the approach with reassigning (
.index
,.columns
attributes).
¡Gracias por tus comentarios!