2016-08-26 83 views
7

我想刪除熊貓數據幀中的所有數據,但得到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 
+3

我喜歡@艾漢的解決方案,但我認爲'DF = pd.DataFrame(列= df.columns)'會更快,更高效... – MaxU

+0

與@同意MaxU - 實際上大約快100倍(1M行和10列的測試數據幀) – exp1orer

+1

'df.iloc [0:0]'實際上比df結構快。我想你是比較它而不是iloc? – ayhan

回答

13

您需要通過要刪除的標籤。

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

默認情況下,它在axis=0上運行。

可以達到同樣的用

df.iloc[0:0] 

這是更有效的。

3

我最喜歡的:

df = df.iloc[0:0] 

但要注意df.index.max()將楠。 要新增項目使用:

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