pandas Python:将excel数据转换为数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44070938/
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 03:38:25  来源:igfitidea点击:

Python: convert excel data into dataframes

pythonexcelpandasdataframe

提问by Guillaume

I want to put some data available in an excel file into a dataframe in Python. The code I use is as below (two examples I use to read an excel file):

我想将 Excel 文件中的一些可用数据放入 Python 中的数据框中。我使用的代码如下(我用来读取excel文件的两个示例):

d=pd.ExcelFile(fileName).parse('CT_lot4_LDO_3Tbin1')
e=pandas.read_excel(fileName, sheetname='CT_lot4_LDO_3Tbin1',convert_float=True)

The problem is that the dataframe I get has the values with only two numbers after comma. In other words, excel values are like 0.123456 and I get into the dataframe values like 0.12.

问题是我得到的数据帧的值在逗号后只有两个数字。换句话说,excel 值就像 0.123456,我进入了像 0.12 这样的数据框值。

A round up or something like that seems to be done, but I cannot find how to change it.

一轮或类似的事情似乎已经完成,但我找不到如何改变它。

Can anyone help me?

谁能帮我?

thanks for the help !

谢谢您的帮助 !

回答by Leonardo Ferreira

Using pandas 0.20.1 something like this should work:

使用 Pandas 0.20.1 应该可以:

df = pd.read_csv('CT_lot4_LDO_3Tbin1.fileformat')

for exemple, in excel:

例如,在excel中:

df = pd.read_csv('CT_lot4_LDO_3Tbin1.xlsx')

Read this documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

阅读此文档:http: //pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

回答by Guillaume

Well, when I try:

好吧,当我尝试:

df = pd.read_csv(r'my file name')

I have something like that in df

我在 df 中有类似的东西

http://imgur.com/a/Q2upp

http://imgur.com/a/Q2upp

And I cannot put .fileformat in the sentence

我不能把 .fileformat 放在句子中

回答by Chih-Hsu Hyman Lin

You can try this. I used test.xlsxwhich has two sheets, and 'CT_lot4_LDO_3Tbin1' is the second sheet. I also set the first value as Textformat in excel.

你可以试试这个。我使用test.xlsx它有两张纸,“CT_lot4_LDO_3Tbin1”是第二张纸。我还在Textexcel中将第一个值设置为格式。

import pandas as pd 
fileName = 'test.xlsx'
df = pd.read_excel(fileName,sheetname='CT_lot4_LDO_3Tbin1')

Result:

结果:

In [9]: df
Out[9]: 
       Test
0  0.123456
1  0.123456
2  0.132320

Without seeing the real raw data file, I think this is the best answer I can think of.

没有看到真正的原始数据文件,我认为这是我能想到的最好的答案。

回答by synaptikon

You might be interested in removing column datatype inference that pandas performs automatically. This is done by manually specifying the datatype for the column. Here is what you might be looking for.

您可能对删除 Pandas 自动执行的列数据类型推断感兴趣。这是通过手动指定列的数据类型来完成的。这就是您可能正在寻找的内容。

Python pandas: how to specify data types when reading an Excel file?

Python pandas:读取Excel文件时如何指定数据类型?