showkey
showkey

Reputation: 298

How to display header in a Series of pandas?

data is a instance of pandas.core.series.Series.

 >>> type(data)
<class 'pandas.core.series.Series'>
>>> data
1    002728
2    002142
3    002284
Name: scode, dtype: object

How to display it as follows ?

>>> data
     scode
1    002728
2    002142
3    002284
Name: scode, dtype: object

Upvotes: 5

Views: 13250

Answers (2)

Martin Cronje
Martin Cronje

Reputation: 467

My answer is not 100% related to the question, but might be valueable to others finding this on a google search.

In the cause you have a Series which is a subset from a dataframe by using the index number you can get the columns by simply adding the keys() function on the series.

dataframe = pd.DataFrame({'A' : [1,2,3], 'B' : [4,5,6]})

for index in range(0, len(dataframe)):
    series = dataframe.iloc[index]
    
type(series)
>> <class 'pandas.core.series.Series'>
    
list(series.keys())
>> ['A', 'B']

Upvotes: 0

Dave Rosenman
Dave Rosenman

Reputation: 1467

You can convert it to a dataframe. Two options on how to do so:

import pandas as pd
data = pd.Series(['002728','002142','002284'], name = 'scode')
data = data.to_frame()
print(data)
   scode
0  002728
1  002142
2  002284

or

import pandas as pd
data = pd.Series(['002728','002142','002284'], name = 'scode')
data = pd.DataFrame(data)
print(data)
 scode
0  002728
1  002142
2  002284

The only practical difference between a single column dataframe and a series that I can think of off the top of my head is indexing. If you want to select the first element of a series... you can do it as follows:

data = pd.Series(['002728','002142','002284'], name = 'scode')
data[0]
# 002728

But for a one column dataframe, data[0] wouldn't work. Here's what you'd need to do to get the value in the first row:

data = pd.Series(['002728','002142','002284'], name = 'scode')
data = data.to_frame()
data.iloc[0,0]
# 002728

And to get the value in the ith row

data = pd.Series(['002728','002142','002284'], name = 'scode')
data = data.to_frame()
print(data.iloc[i,0])

You could use

data = pd.Series(['002728','002142','002284'], name = 'scode')
data = data.to_frame()
data.iloc[i]

but that would give you a series containing just the value in the ith row.

print(type(data.iloc[0,0]))
#<class 'str'>
print(type(data.iloc[0]))
#pandas.core.series.Series

If your series consisted of numerical values...here's how a vectorized method such as multiplication would work:

numbers = pd.Series([1,3,5,7], name = 'numbers')
print(numbers)
# 0  1
1    3
2    5
3    7
Name: numbers, dtype: int64


print(numbers*3)
#0    3
1     9
2    15
3    21
Name: numbers, dtype: int64

For a single column dataframe with the same numerical values as the series above:

numbers = pd.Series([1,3,5,7], name = 'numbers')
numbers = numbers.to_frame()
print(numbers)
#   numbers
0        1
1        3
2        5
3        7

print(numbers*3)

#   numbers
0        3
1        9
2        15
3        21

Upvotes: 3

Related Questions