Python Pandas 丢弃数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47704798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:52:15  来源:igfitidea点击:

Python Pandas Drop Dataframe

pythonpython-3.xpandasdataframedata-science

提问by HenryHub

How do I delete a column from a DataFrame? I know this data is not reproducible as I have a CSV file and I am trying to build a pandas data frame to do some wrangling.

如何从 DataFrame 中删除列?我知道这个数据是不可重现的,因为我有一个 CSV 文件,我正在尝试构建一个 Pandas 数据框来做一些争论。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('C:\LoadProfiles\CSV\WillBaySchl 2013_2014 KW.csv')

print(df)

This will return the head/tail and:[34944 rows x 3 columns]

这将返回头部/尾部和:[34944 rows x 3 columns]

pos0 = 0
pos1 = 1
pos2 = 2

colname = df.columns[pos0]
print(colname)

This will return: Meter ID(I want to drop this column/dataframe)

这将返回:(Meter ID我想删除此列/数据框)

colname = df.columns[pos1]
print(colname)

This will return: Date / Time(I want this to be the pd data frame index)

这将返回:(Date / Time我希望这是 pd 数据框索引)

colname = df.columns[pos2]
print(colname)

This will return: KW(ch: 1 set:0)(This is the data that I want to rename "kW" and do some wrangling...)

这将返回:(KW(ch: 1 set:0)这是我想重命名“kW”并进行一些争论的数据......)

If I try this code below:

如果我尝试下面的代码:

df = pd.DataFrame.drop(['Meter ID'], axis=1)

print(df)

Python will return the error:TypeError: drop() missing 1 required positional argument: 'labels'

Python 将返回错误:TypeError: drop() missing 1 required positional argument: 'labels'

If I try this code below:

如果我尝试下面的代码:

df = pd.DataFrame.drop(columns=['Meter ID'])
print(df)

Python will return the error: TypeError: drop() got an unexpected keyword argument 'columns'

Python 将返回错误: TypeError: drop() got an unexpected keyword argument 'columns'

Any help is greatly appreciated...

任何帮助是极大的赞赏...

回答by avchauzov

If I understand right to delete column (single) you should use:

如果我理解删除列(单个)的权利,您应该使用:

df = pd.DataFrame.drop('Meter ID', axis=1)

For more than 1 column:

对于超过 1 列:

df = pd.DataFrame.drop(['Meter ID', 'abc'], axis=1)

Difference is in [] brackets.

区别在于 [] 括号中。

To delete the whole df you can use either (as mentioned already):

要删除整个 df 您可以使用(如前所述):

del df

or

或者

df = None

回答by Shubham Sharma

After reading your question , what i understand is you wanted to drop column ['Meter ID']available in your df = pd.read_csv('C:\LoadProfiles\CSV\WillBaySchl 2013_2014 KW.csv')pandas dataframe . I am assuming that you have column name ['Meter ID']like these in your dataframe and also as header in your csv file .

阅读您的问题后,我的理解是您想删除Pandas 数据框中['Meter ID']可用的列df = pd.read_csv('C:\LoadProfiles\CSV\WillBaySchl 2013_2014 KW.csv')。我假设['Meter ID']你的数据框中有这样的列名,也有你的 csv 文件中的标题。

>>> df.dtypes
Meter ID           int64
someothercolumn    int64
dtype: object

for that you can simply use these code ,

为此,您可以简单地使用这些代码,

del df['Meter ID']

Now if you wanted to delete overall dataframe you can simply use these code,

现在如果你想删除整个数据框,你可以简单地使用这些代码,

df=None

回答by Vivek Harikrishnan

To drop a column from dataframe,

要从数据框中删除一列,

df = df.drop('Meter ID', axis=1)

Drop more than one columns at a time,

一次删除不止一列,

df = df.drop(['Meter ID', 'SomethingElse'], axis=1)

For more pandas.DataFrame.drop

欲了解更多pandas.DataFrame.drop

回答by Mutyalama

To lose the 'Meter ID' column you could also use:

要丢失“仪表 ID”列,您还可以使用:

df = df.drop(columns=['Meter ID']) 

post pandas version 0.21

发布Pandas版本 0.21