gpt4 book ai didi

python - 如何消除Python数据框中的列和行的索引值?

转载 作者:行者123 更新时间:2023-12-01 08:00:47 24 4
gpt4 key购买 nike

我试图消除表示值索引的列和行,并将列的第一个值替换为“kw”。

尝试过.drop但没有成功

def main():

df_old_m, df_new_m = open_file_with_data_m(file_old_m, file_new_m)
#combine two dataframes together
df = df_old_m.append(df_new_m)
son=df["son"]
gson=df["gson"]
#add son
df_old_m = df["seeds"].append(son)
#add gson
df_old_m = df_old_m.append(gson)
#deleted repeated values
df_old_m = df_old_m.drop_duplicates()

#add kw as the header of the list
df_old_m.loc[-1] = 'kw' # adding a row
df_old_m.index = df_old_m.index + 1 # shifting index
df_old_m.sort_index(inplace=True)

这给了我 .xlsx output

最佳答案

如果 kw 是您想要作为新索引的列,您可以执行以下操作:

df.set_index('kw', inplace=True)

关于python - 如何消除Python数据框中的列和行的索引值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55745220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com