Reputation: 5559
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?
Upvotes: 2
Views: 9017
Reputation: 14075
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
Upvotes: 2
Reputation: 667
Another method would be:
pd.DataFrame({'Start':[None],'End':[None],'Duration':[None]})
Upvotes: 2
Reputation: 862661
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
Upvotes: 2