Tag: pandas

pandas:将特定的Excel单元格值读入一个variables

情况: 我使用pandas来parsing工作簿中的单独Excel( .xlsx )工作表,使用以下设置: Python 3.6.0和Windows 7 x64.上的Anaconda 4.3.1 Windows 7 x64. 问题: 我一直无法find如何设置一个variables到一个特定的Excel工作表单元格值,例如var = Sheet['A3'].value 'Sheet2'使用pandas ? 题: 这可能吗? 如果是这样,怎么样? 我曾经尝试过: 我已经通过pandas在dataframe和各种论坛上的文档进行了search,但没有find答案。 我知道我可以解决这个使用openpyxl (我可以指定一个单元格坐标),但我想: 使用pandas – 如果可能的话; 只能在文件中读取一次。 我input了numpy ,以及pandas ,所以能够写: xls = pd.ExcelFile(filenamewithpath) data = xls.parse('Sheet1') dateinfo2 = str(xls.parse('Sheet2', parse_cols = "A", skiprows = 2, nrows = 1, header = None)[0:1]).split('0\n0')[1].strip() 'Sheet1'被读入'data'是好的,因为我有一个函数来收集我想要的范围。 我也试图从单独的表格( […]

将分组的项目保存到不同的Excel表单

我有一个excel文件,我想根据列名“步骤号”进行分组。 并想要相应的值。这里是我写的一段代码: import numpy as np import pandas as pd import matplotlib.pyplot as plt fpath=('/Users/Anil/Desktop/Test data.xlsx') df=pd.read_excel(fpath) data=df.loc[:,['Step No.','Parameter','Values']] grp_data=pd.DataFrame(data.groupby(['Step No.','Values']).size().reset_index()) grp_data.to_excel('/Users/Anil/Desktop/Test1 data.xlsx') 数据被分组,就像我想要的那样。 Step No. Values 1 62 1 62.5 1 63 1 66.5 1 68 1 70 1 72 1 76.5 1 77 2 66.5 2 67 2 69 3 75.5 3 77 […]

将多个系列词典保存到Excel

我有一个101列的数据框,我想看看我的DataFrame中的每个variables的分布。 使用pandasvalue_counts我创build了多个长度的几个系列字典。 每个系列都有自己的钥匙。 首先我做: out={} for c in df.columns: out[c]=df[c].value_counts(dropna=False).fillna(0) 所以,out是一个大小为101的字典。在out内是一系列不同大小的字典。 Key | Type | Size | Value Key1 Series (12,) class 'pandas.core.series.Series' Key2 Series (7,) class 'pandas.core.series.Series' Key3 Series (24,) class 'pandas.core.series.Series' . . . Key101 每个密钥都是唯一的。 我想将这些系列中的每一个保存到一个Excel文件中。 这个答案是非常接近的,并且对于循环中的第一个键是有效的,但是它不会继续到字典中的下一个键。 这是我现在所拥有的: for key in out.keys(): s=out[key] name=key[:30] s.to_excel('xlfile.xlsx', sheet_name=name) 我只保留前30个字符,因为这是Excel表格名称的限制。 我不需要他们自己的工作表,我宁愿他们都被保存到一个单一的工作表,但这是最接近我可以保存他们。 显然是一个新手,所以如果有更好的方法来解决我的根本问题,那也太棒了。 我接受任何build议,感谢您的时间。

在pandas数据框中插入值

我有一个Excel工作表中的数据。 我想检查一个范围的一列值,如果该值在该范围(5000-15000),那么我想插入另一列(正确或标志)的价值。 我有三列:城市,租金,地位。 我试过追加和插入方法,但没有奏效。 我应该怎么做? 这是我的代码: 对于索引,在df.iterrows()中的行: if row['city']=='mumbai': if 5000<= row['rent']<=15000: pd.DataFrame.append({'Status': 'Correct'}) 它显示这个错误: TypeError:append()缺less1个需要的位置参数:'other' 我应该遵循什么程序在一列中逐行插入数据?

如何让大pandas创build新的表而不是覆盖?

我正在为自己的工作build立一个自动报告工具。 我试图让我的代码工作,每次(每一天)我运行程序并生成报告创build另一个工作表。 date_time = time.strftime('%b %d %Y') writer = pd.ExcelWriter('BrokerRisk.xlsx', engine='xlsxwriter') df.to_excel(writer,'DataFrame-' + date_time) sums.to_excel(writer,'TotalByCounterparty-' + date_time) sums_sort.to_excel(writer,'SortedRank-' + date_time) workbook = writer.book worksheet1 = writer.sheets['DataFrame-' + date_time] worksheet2 = writer.sheets['TotalByCounterparty-' + date_time] worksheet3 = writer.sheets['SortedRank-' + date_time] writer.save() 我试图实现datefunction,以便它会每天在技术上改变名称,但是这似乎也不起作用。 谁能提出一个简单的解决

编写大单元格值(公式)时出现exception

我正在使用xlwings在Excel中更新大公式。 我不知道如何禁用与xlwings交互模式,并不像我受到这种影响: pywin32和excel。 写入大量数据时出现exception 当我尝试从pandas数据框中写入form =“ABC”+“DEF”+“XYZ”的巨大公式时,如何debugging下面的exception? 如何find根本原因? pywintypes.com_error:(-2147352567,'Exception occurred。',(0,None,None,None,0,-2146827284),None) 谢谢。

如何用数据框创build不确定长度的Excel电子表格?

我是一个Python用户的新用户,我一直在写一个程序,我需要创build一个不确定数量的列的Excel电子表格。 所以我之前的代码创build了4列: writer = pd.ExcelWriter(datapath + 'Test#' + str(testcount) + '.xlsx', engine = 'xlsxwriter') df1 = pd.DataFrame({'Species 1' : evolution[0]}) df2 = pd.DataFrame({'Species 2' : evolution[1]}) df3 = pd.DataFrame({'Species 3' : evolution[2]}) df4 = pd.DataFrame({'Species 4' : evolution[3]}) df1.to_excel(writer, sheet_name='Raw Data') df2.to_excel(writer, sheet_name='Raw Data', startcol=2, index=False) df3.to_excel(writer, sheet_name='Raw Data', startcol=3, index=False) df4.to_excel(writer, sheet_name='Raw Data', startcol=4, […]

用xlwings读取Excel表头

我如何使用xlwings读取excel中的“表格”,将其转换为pandas DataFrame,其中表格“标题”成为DataFrame列名称? 我试图读取表格的每一个方法,标题行总是被排除在读取之外! 这是我试过的,其中“b”是我的xlwings工作簿对象: b.sheets['Sheet1'].range('Table1').options(pd.DataFrame) b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=False) b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=True)

当它不在Excel列中时,如何从Pandas df列中删除时间?

我已经将两个工作表分别读入了pandas数据框。 两者都有date发布的列。 在这两个工作表中,该列以exce / dd / mm / yyyy保存。 WS1 13/02/2017 01/02/2017 08/11/2016 05/08/2016 16/03/2017 53 2017-02-13 51 2017-02-01 22 2016-11-08 0 2016-08-05 63 2017-03-16 Name: Date Issued, dtype: datetime64[ns] 但是ws2 08/03/2017 24/08/2016 28/11/2016 26/10/2016 10/03/2017 0 2017-03-08 00:00:00 1 2016-08-24 00:00:00 2 2016-11-28 00:00:00 3 2016-10-26 00:00:00 4 2017-03-10 00:00:00 Name: Date Issued, dtype: […]

string拆分在单个string上工作,但不是在pandas系列的string

我对Python和大pandas很新,有一个问题。 我有一系列需要编辑的45398个string。 我从一个Excel文件导入他们。 import pandas as pd import numpy as np import xlrd file_location = "#mypath/leistungen_2017.xlsx" workbook = xlrd.open_workbook(file_location) sheet = workbook.sheet_by_index(0)` df = pd.read_excel("leistungen_2017.xlsx") 这里是前几行,就像例子。 >>> df Leistungserbringer Anzahl Leistung Code Rechnungsnummer 0 Albert 1 15.0160 Vollständige Spirometrie und Resistanc… 1 8957 1 Albert 1 15.0200 CO-Diffusion, jede Methode 1 8957 2 Albert 1 […]