pandas 如何在一系列熊猫中显示标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47736692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:52:31  来源:igfitidea点击:

How to display header in a Series of pandas?

pandasheaderseries

提问by coder_view

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

data 是 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

回答by David Rosenman

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[0] 不起作用。以下是获取第一行中的值所需的操作:

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

并获取第 i 行中的值

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.

但这会给你一个只包含第 i 行值的系列。

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