使用None初始化pandas数据帧

时间:2017-03-07 09:57:50

标签: python pandas dataframe

我很难创建一个无值的DataFrame。 为此,我执行了几个步骤,但我相信我可以使用熊猫来获得相同的结果'功能......

mydata = []
mydata.append([None, None, None, None])
mydata = np.array(mydata)
mydata = pd.DataFrame(mydata, columns='Start','End','Duration'])   

是否有命令可以获得相同的结果?

3 个答案:

答案 0 :(得分:1)

我认为您需要numpy array创建的reshape list

mydata = pd.DataFrame(np.array([None, None, None]).reshape(-1,3), 
                      columns=['Start','End','Duration'])   
print (mydata)
  Start   End Duration
0  None  None     None

使用[[]]的另一个更慢的解决方案:

mydata = pd.DataFrame([[None, None, None]], columns=['Start','End','Duration'])   
print (mydata)
  Start   End Duration
0  None  None     None

如果使用columnsindex值,则所有数据均为NaNNone可能replaceprint (pd.DataFrame(columns=['Start','End','Duration'], index=[0])) Start End Duration 0 NaN NaN NaN mydata = pd.DataFrame(columns=['Start','End','Duration'], index=[0]).replace({np.nan:None}) print (mydata) Start End Duration 0 None None None

Card succesfully initialized.

Card size: 2008 MB (MB = 1,000,000 bytes)

Volume is FAT16, Cluster size (bytes): 32768

Files found (date time size name) :

Success! Type any character to restart.

答案 1 :(得分:0)

另一种方法是:

pd.DataFrame({'Start':[None],'End':[None],'Duration':[None]})

答案 2 :(得分:0)

这是一个快速的单行:

>>> pd.DataFrame(np.empty((4,3),dtype=pd.Timestamp),columns=['Start','End','Duration'])
  Start   End Duration
0  None  None     None
1  None  None     None
2  None  None     None
3  None  None     None

一般情况下,单行将采用:

>>> pd.DataFrame(np.empty((5,3),dtype=object),columns=['Start','End','Duration'])
  Start   End Duration
0  None  None     None
1  None  None     None
2  None  None     None
3  None  None     None
4  None  None     None

这是NaN单行:

>>> pd.DataFrame(np.empty((2,3))*np.nan,columns=['Start','End','Duration'])   
   Start  End  Duration
0    NaN  NaN       NaN
1    NaN  NaN       NaN