当我将pandas Series插入数据框时,所有值都变为NaN [英] When I insert pandas Series into dataframe, all values become NaN

查看:788
本文介绍了当我将pandas Series插入数据框时,所有值都变为NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的熊猫系列:

I have a pandas series that looks like this:

>>> myseries 2012-01-01 15:20:00-05:00 2 2012-01-01 15:30:00-05:00 1 2012-01-01 15:40:00-05:00 0 ...

>>> myseries 2012-01-01 15:20:00-05:00 2 2012-01-01 15:30:00-05:00 1 2012-01-01 15:40:00-05:00 0...

然后我尝试将其放入数据框:

And I try to put it into a dataframe as so:

>>> mydf = pd.DataFrame(myseries, columns=["myseries"], index = myseries.index)

由于某种原因,所有值都变为NaN:

and all the values become NaN for some reason:

>>> mydf 2012-01-01 15:20:00-05:00 NaN 2012-01-01 15:30:00-05:00 NaN 2012-01-01 15:40:00-05:00 NaN

>>> mydf 2012-01-01 15:20:00-05:00 NaN 2012-01-01 15:30:00-05:00 NaN 2012-01-01 15:40:00-05:00 NaN

我很困惑.这似乎是一个非常简单的应用程序.我究竟做错了什么?顺便说一句,用pd.DataFrame(myseries.values, columns=...)替换可以解决此问题,但是为什么有必要呢?谢谢.

I'm pretty confused. This seems like a really simple application. What am I doing wrong? By the way, replacing with pd.DataFrame(myseries.values, columns=...) fixes the problem, but why is it necessary? Thank you.

推荐答案

更简单:

s = pd.Series([0,1,2,3], index=pd.date_range('2014-01-01', periods=4), name='s')
df = pd.DataFrame(s)
print(df)

收益

            s
2014-01-01  0
2014-01-02  1
2014-01-03  2
2014-01-04  3

这篇关于当我将pandas Series插入数据框时,所有值都变为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆