2

I am having an hard time to create a DataFrame with None values. To do so I execute several steps, but I believe that I can get the same results using a pandas' function...

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

Is there a command to get the same results?

3 Answers 3

2

I think you need reshape numpy array created from 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

Another slowier solution with [[]]:

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

If use columns and index values, all data are NaN and is possible replace them to None:

print (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
Sign up to request clarification or add additional context in comments.

Comments

2

Another method would be:

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

Comments

2

Here is a fast one-liner:

>>> 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

In general, an one-liner would go as:

>>> 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

Here is a NaN one-liner:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.