gpt4 book ai didi

python - 如何根据python(pandas)中的用户输入删除列?

转载 作者:行者123 更新时间:2023-12-04 07:39:24 30 4
gpt4 key购买 nike

如何编写代码以便数据框根据用户输入的列名删除列?

amend_col= input(" would u like to 'remove' any column?:")
if amend_col == "yes":
s_col= input("Select the column you want to add or remove:")
if s_col in df.columns:
print("Column is found and removed")
df.drop(df[s_col])
else:
print("No columns removed")

最佳答案

您可以按如下方式修改代码:

amend_col= input(" would u like to 'remove' any column?:")
if amend_col == "yes":
s_col= input("Select the column you want to add or remove:")
if s_col in df.columns and amend_col =="yes":
print("Column is found and removed")
df = df.drop(columns=s_col)
else:
print("No columns removed")
你的代码很接近,我们只需要替换行 df.drop(df[s_col])
df = df.drop(columns=s_col)

关于python - 如何根据python(pandas)中的用户输入删除列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67573652/

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