Answers for "remove default index from pandas dataframe"

0

remove default index from pandas dataframe

pythonCopyimport pandas as pd

my_df = pd.DataFrame({
    'Person': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'City': ['Berlin', 'Montreal', 'Toronto', 'Rome', 'Munich'],
    'Mother Tongue': ['German', 'French', 'English', 'Italian', 'German'],
    'Age':  [37, 20, 38, 23, 35],

},index=["A","B","C","D","E"])

df_reset=my_df.set_index('Person')

print("Initial DataFrame:")
print(my_df,"\n")

print("After setting Person column as Index:")
print(df_reset)
Posted by: Guest on September-21-2021
0

remove default index from pandas dataframe

pythonCopyInitial DataFrame:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After setting Person column as Index:
             City Mother Tongue  Age
Person
Alice      Berlin         German   37
Steven   Montreal         French   20
Neesham   Toronto        English   38
Chris        Rome        Italian   23
Alice      Munich         German   35
Posted by: Guest on September-21-2021

Code answers related to "remove default index from pandas dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language