简体   繁体   English

空的熊猫数据框填充随机值,如何使其全部为 NaN?

[英]Empty pandas dataframe fills with random values, how to make it all NaN?

I am trying to make an empty dataframe with pandas and numpy in python, so a dataframe filled with NaN, but everytime it seems to fill up with values.我试图在 python 中用 Pandas 和 numpy 创建一个空数据框,所以一个数据框填充了 NaN,但每次它似乎都填充了值。 Why is this happening and how do I make an empty?为什么会发生这种情况,我该如何清空?

This is what I already tried:这是我已经尝试过的:

data_class_b = pd.DataFrame(np.NaN, index=range(len(df)), columns=[0,1])

尝试 1

data_class_b = pd.DataFrame(np.empty((len(df),2)))

尝试 2

The methods that I used are based on answers of questions about making empty dataframes, in which they state they should work.我使用的方法是基于有关制作空数据框的问题的答案,他们声明它们应该工作。 But I don't get the desired results.但我没有得到想要的结果。 So how do I make an empty dataframe?那么如何制作一个空的数据框呢?

Try remove np.nan :尝试删除np.nan

data_class_b = pd.DataFrame(index=range(len(df)), columns=[0,1])

But for me this working correct too:但对我来说,这个工作也正确:

data_class_b = pd.DataFrame(np.NaN, index=range(len(df)), columns=[0,1])

I restarted my laptop and now it works, so I still don't know why it was not working before but it works now.我重新启动了我的笔记本电脑,现在它可以工作了,所以我仍然不知道为什么它以前不工作,但现在可以工作了。 Before I posted the question I already restarted the python program, but that didn't do it.在我发布问题之前,我已经重新启动了 python 程序,但没有这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM