Pandas DataFrame 删除索引
本教程将介绍如何删除 Pandas DataFrame 的索引。
我们将使用下面显示的 DataFrame 来展示如何删除索引。
import 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"],
)
print(my_df)
输出:
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
使用 reset_index()
方法删除 Pandas DataFrame 的索引
pandas.DataFrame.reset_index()
会将 DataFrame 的索引重置为默认索引。
import 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.reset_index()
print("Before reseting Index:")
print(my_df, "\n")
print("After reseting Index:")
print(df_reset)
输出:
Before reseting Index:
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 reseting Index:
index Person City Mother Tongue Age
0 A Alice Berlin German 37
1 B Steven Montreal French 20
2 C Neesham Toronto English 38
3 D Chris Rome Italian 23
4 E Alice Munich German 35
它将重置 DataFrame 的索引,但现在的索引将显示为 index
列。如果我们想删除 index
列,我们可以在 reset_index()
方法中设置 drop=True
。
import 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.reset_index(drop=True)
print("Before reseting Index:")
print(my_df, "\n")
print("After reseting Index:")
print(df_reset)
输出:
Before reseting Index:
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 reseting Index:
Person City Mother Tongue Age
0 Alice Berlin German 37
1 Steven Montreal French 20
2 Neesham Toronto English 38
3 Chris Rome Italian 23
4 Alice Munich German 35
使用 set_index()
方法删除 Pandas DataFrame 的索引
pandas.DataFrame.set_index()
将把作为参数传递的列设置为 DataFrame 的索引,覆盖初始索引。
import 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)
输出:
Initial 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
它将 Person
列设置为 my_df
DataFrame 的索引,覆盖了 DataFrame 的初始索引。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:114 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:55 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:181 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:190 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:140 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:51 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串