简体   繁体   English

Python - 从 Pandas 输出中消除索引值

[英]Python - Eliminate index values from Pandas output

CODE SAMPLE代码示例

import pandas as pd
data = pd.read_csv('file_name.csv',parse_dates=True)
print(data['EmpName'])

Output:输出:

**0**     Tim K
**1**     Joesph R
**2**     Don T
**3**     Rachel P
**4**     Sam W

Want to eliminate index values from the output?想要从输出中消除索引值?

If you want to print out a list of names use:如果要打印名称列表,请使用:

for i in data['EmpName']:
    print(i)

Outputs:输出:

Tim K
Joseph R
...

If you need the data in a list use:如果您需要列表中的数据,请使用:

new_list = list(data['EmpName'])

Final note最后说明

Print statement formatting is somewhat arbitrary.打印语句格式有点随意。 Usually the print function is used to test objects during development.通常打印功能用于在开发过程中测试对象。 You should familiarize yourself with common python objects such as lists, tuples, numpy arrays and pandas dataframes, to enable you to manipulate data effectively.您应该熟悉常见的 Python 对象,例如列表、元组、numpy 数组和 Pandas 数据帧,以使您能够有效地操作数据。

data['EmpName'] is a Pandas Series. data['EmpName']是 Pandas 系列。 If you do list([data['EmpName']) , that will print it out as a list;如果您执行list([data['EmpName']) ,则会将其打印为列表; it will be enclosed in brackets and the elements will be separated with commas.它将被括在括号中,元素将用逗号分隔。 So it will look like this: ['Tim K', 'Joesph R', 'Don T', 'Rachel P', 'Sam W'] .所以它看起来像这样: ['Tim K', 'Joesph R', 'Don T', 'Rachel P', 'Sam W'] If you don't want the commas, you can do ' '.join(data['EmpName']) and that will give 'Tim K Joesph R Don T Rachel P Sam W' .如果你不想要逗号,你可以做' '.join(data['EmpName']) ,这会给'Tim K Joesph R Don T Rachel P Sam W' Note that if any of the elements are not string-type (eg some are int), that will give an error, so to be safe you might want to do ' '.join([name for name in data['EmpName']])请注意,如果任何元素不是字符串类型(例如有些是 int),则会出现错误,因此为了安全起见,您可能需要执行' '.join([name for name in data['EmpName']])

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

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