pandas 在python中删除列

inn6fuwd  于 2022-12-09  发布在  Python
关注(0)|答案(1)|浏览(154)

我已经编写了这段代码来删除数据集中的列,但消息是

df = Data.drop('URL', axis=1)

----> 1 df = Data.drop('URL', axis=1)

AttributeError: '_io.TextIOWrapper' object has no attribute 'drop'

我想用Pandas来代替这个专栏,但没有成功

jei2mxaa

jei2mxaa1#

嗨,👋希望你一切顺利!
看起来Data的类型为_io.TextIOWrapper,但它应该仅为DataFrame类型,这样您就可以删除列:

import pandas as pd

dummy_df = pd.DataFrame(
    {
        "A": [1, 2, 3],
        "B": [4, 5, 6],
        "C": [7, 8, 9]
    },
)

print(dummy_df)
#   A  B  C
# 0  1  4  7
# 1  2  5  8
# 2  3  6  9

dummy_df = dummy_df.drop(columns=["B", "C"])

print(dummy_df)
#   A
# 0  1
# 1  2
# 2  3
  1. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

相关问题