Python 如何在熊猫中使用就地保存修改?
如果使用此代码删除列实体: t、 删除“实体”,轴=1,在位=True 然后,前面的数据帧再次出现在这里。那么,如何使用inplace=True保存修改,以便删除实体列呢Python 如何在熊猫中使用就地保存修改?,python,python-3.x,pandas,jupyter-notebook,Python,Python 3.x,Pandas,Jupyter Notebook,如果使用此代码删除列实体: t、 删除“实体”,轴=1,在位=True 然后,前面的数据帧再次出现在这里。那么,如何使用inplace=True保存修改,以便删除实体列呢 你可以两个都来一次 rd.drop[实体,代码],轴=1,在位=True 不清楚你想放弃什么。如果使用axis=1,则尝试删除列。。。在这种情况下,看起来您的代码工作正常,我没有看到数据帧输出中的列代码 如果您试图根据Entity的值删除行,则需要使用df.loc的索引,如中所示 `df.drop(df.loc[df['col
你可以两个都来一次 rd.drop[实体,代码],轴=1,在位=True
不清楚你想放弃什么。如果使用axis=1,则尝试删除列。。。在这种情况下,看起来您的代码工作正常,我没有看到数据帧输出中的列代码 如果您试图根据Entity的值删除行,则需要使用df.loc的索引,如中所示
`df.drop(df.loc[df['columnsname']=="Entity"].index, inplace=True)`
或
以此为例
from dataframefromstring import DataFrameFromString
df = DataFrameFromString() # Just loads a df for this example. Nothing to do with your code
# show the starting dataframe
print("=== Original DF ==============")
print(df)
# Drop the column "Name"
df.drop('Name', axis=1, inplace=True)
print("=== Dropped column ==============")
print(df)
# Drop multiple columns
df.drop(['Ticket_No', "Fare"], axis=1, inplace=True)
print("=== Dropped mutiple columns ==============")
print(df)
# Drop rows based on column X having some value
df.drop(df.loc[df['Sex']=="male"].index, inplace=True)
print("=== Drop row where Column = <something> ==============")
print(df)
# Drop rows based on multiple values value
df.drop(df.loc[ (df['Sex'].isnull() ) | (df["Age"] < 42 ) ].index, inplace=True)
print("=== Drop rows based on multiple conditions ==============")
print(df)
输出:
您的t.drop'Entity',axis=1,inplace=True的代码是正确的 在运行drop命令之前,您还可以向我们显示数据帧吗?据我所见,您的代码似乎运行良好。另外,请将您的代码、数据和所需输出以缩进文本而不是图片的形式放入问题中,以便我们可以重新创建问题。
from dataframefromstring import DataFrameFromString
df = DataFrameFromString() # Just loads a df for this example. Nothing to do with your code
# show the starting dataframe
print("=== Original DF ==============")
print(df)
# Drop the column "Name"
df.drop('Name', axis=1, inplace=True)
print("=== Dropped column ==============")
print(df)
# Drop multiple columns
df.drop(['Ticket_No', "Fare"], axis=1, inplace=True)
print("=== Dropped mutiple columns ==============")
print(df)
# Drop rows based on column X having some value
df.drop(df.loc[df['Sex']=="male"].index, inplace=True)
print("=== Drop row where Column = <something> ==============")
print(df)
# Drop rows based on multiple values value
df.drop(df.loc[ (df['Sex'].isnull() ) | (df["Age"] < 42 ) ].index, inplace=True)
print("=== Drop rows based on multiple conditions ==============")
print(df)
=== Original DF ==============
Name Sex Age Ticket_No Fare
0 Braund male 22.0 HN07681 2500.0
1 NaN female 42.0 HN05681 6895.0
2 peter male NaN KKSN55 800.0
3 NaN male 56.0 HN07681 2500.0
4 Daisy female 22.0 hf55s44 NaN
5 Manson NaN 48.0 HN07681 8564.0
6 Piston male NaN HN07681 5622.0
7 Racline female 42.0 Nh55146 NaN
8 NaN male 22.0 HN07681 4875.0
9 NaN NaN NaN NaN NaN
=== Dropped column ==============
Sex Age Ticket_No Fare
0 male 22.0 HN07681 2500.0
1 female 42.0 HN05681 6895.0
2 male NaN KKSN55 800.0
3 male 56.0 HN07681 2500.0
4 female 22.0 hf55s44 NaN
5 NaN 48.0 HN07681 8564.0
6 male NaN HN07681 5622.0
7 female 42.0 Nh55146 NaN
8 male 22.0 HN07681 4875.0
9 NaN NaN NaN NaN
=== Dropped mutiple columns ==============
Sex Age
0 male 22.0
1 female 42.0
2 male NaN
3 male 56.0
4 female 22.0
5 NaN 48.0
6 male NaN
7 female 42.0
8 male 22.0
9 NaN NaN
=== Drop row where Column = <something> ==============
Sex Age
1 female 42.0
4 female 22.0
5 NaN 48.0
7 female 42.0
9 NaN NaN
=== Drop rows based on multiple conditions ==============
Sex Age
1 female 42.0
7 female 42.0