Python table gives NaN when initialized with series

0 votes

IF I am using this code:

a=array([2,2,2,3])
c=DataFrame({"A":1,
"B":Series(a),
"C":Timestamp("20130203"),
"D":array([3]*4),
"E":"Elephant"},index=["Firstyear","Secondyear","Thirdyear","Fourthyear"])
print(c)

I am getting the following output

A B C D E
Firstyear 1 NaN 2013-02-03 3 Elephant
Secondyear 1 NaN 2013-02-03 3 Elephant
Thirdyear 1 NaN 2013-02-03 3 Elephant
Fourthyear 1 NaN 2013-02-03 3 Elephant

But why is the B table giving Nan output and not the "a" array?

Jul 9 in Python by Gani
14 views

1 answer to this question.

0 votes

The NaN value you are getting is because you are trying to print both keys and values at a time for the series ---> a, this is turning out to be Nan :

I have done modification in the code ,this is generating the output as required :

import numpy as np

import pandas as pd

a=np.array([2,2,2,3],dtype="int")

data= ({"A":1,

"B":pd.Series(a).values,

"C":pd.Timestamp("20130203"),

"D":np.array([3]*4),

"E":"Elephant"})

df = pd.DataFrame(data, index=["Firstyear","Secondyear","Thirdyear","Fourthyear"]) 

print (df)

image

answered Jul 9 by Yogi

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How to perform web scraping with python?

Hey, there are various libraries used in ...READ MORE

answered Apr 20, 2018 in Python by aayushi
• 750 points
203 views
0 votes
1 answer

Obtaining a value when given a key in python dicionaries

We cannot. Dictionaries aren't meant to be ...READ MORE

answered May 31, 2018 in Python by Nietzsche's daemon
• 4,260 points
22 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,310 points
47 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 views
+4 votes
6 answers
+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 67,120 points
2,340 views
+1 vote
1 answer

How to replace id with attribute corresponding to id of another table?

Use the following query statement and let ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 56,160 points
21 views