Python 删除熊猫数据框中的所有数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39173992/
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-08-19 21:58:02  来源:igfitidea点击:

Drop all data in a pandas dataframe

pythonpython-2.7pandas

提问by user2242044

I would like to drop all data in a pandas dataframe, but am getting TypeError: drop() takes at least 2 arguments (3 given). I essentially want a blank dataframe with just my columns headers.

我想删除 pandas 数据框中的所有数据,但我正在获取TypeError: drop() takes at least 2 arguments (3 given). 我基本上想要一个只有我的列标题的空白数据框。

import pandas as pd

web_stats = {'Day': [1, 2, 3, 4, 2, 6],
             'Visitors': [43, 43, 34, 23, 43, 23],
             'Bounce_Rate': [3, 2, 4, 3, 5, 5]}
df = pd.DataFrame(web_stats)

df.drop(axis=0, inplace=True)
print df

回答by ayhan

You need to pass the labels to be dropped.

您需要传递要删除的标签。

df.drop(df.index, inplace=True)

By default, it operates on axis=0.

默认情况下,它在axis=0.

You can achieve the same with

你可以用

df.iloc[0:0]

which is much more efficient.

这效率更高。

回答by tomatom

My favorite:

我最喜欢的:

df = df.iloc[0:0]

But be aware df.index.max() will be nan. To add items I use:

但请注意 df.index.max() 将为 nan。要添加我使用的项目:

df.loc[0 if math.isnan(df.index.max()) else df.index.max() + 1] = data

回答by Raul Menendez

My favorite way is:

我最喜欢的方式是:

df = df[0:0]