简体   繁体   English

如何使用python pandas在excel电子表格中找到最后一列或最后一行

[英]how do you find the last column or row in an excel spreadsheet using python pandas

Hi am looking to import part of a spreadsheet as a data frame using pandas but the problem is the spreadsheet changes weekly and the number of rows and columns varies each week. 您好,我希望使用熊猫将电子表格的一部分作为数据框导入,但问题是电子表格每周更改一次,并且行和列的数量每周都不同。

In Excel VBA I can programmatically determine the number of columns and rows in an excel spreadsheet, but how do I determine that in python?? 在Excel VBA中,我可以以编程方式确定Excel电子表格中的列和行数,但是如何在python中确定呢?

col-1 | col -2
1.     blue
2.     green
3.     blue
4.     blank
5.     blank

I need some python code preferably a parameter of the python - pandas method read_excel which tells me that the number of columns in the document I pass to it is 2, and the number of rows is 5 or 6 if you include header. 我需要一些python代码,最好是python-pandas方法read_excel的参数,该参数告诉我我传递给它的文档中的列数为2,如果包含标头,则行数为5或6。

However I need this to be programmable (called each time) because I want to feed it spreadsheets of different sizes with different numbers of columns. 但是,我需要对此进行编程(每次调用),因为我想使用不同的列数为它提供不同大小的电子表格。

Use fillna with method 'ffill' fillna与方法'ffill'一起使用

In [71]:

df1 = pd.DataFrame({'a':[1,2,3,NaN,NaN,4,5,6], 'b':[1,2,NaN,NaN, 3,NaN,4,5]})
df1
Out[71]:
    a   b
0   1   1
1   2   2
2   3 NaN
3 NaN NaN
4 NaN   3
5   4 NaN
6   5   4
7   6   5

[8 rows x 2 columns]

In [85]: 在[85]中:

df1.fillna(method='ffill',inplace=True)
df1
Out[85]:
   a  b
0  1  1
1  2  2
2  3  2
3  3  2
4  3  3
5  4  3
6  5  4
7  6  5

[8 rows x 2 columns]

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

相关问题 如何使用熊猫python在excel中为最后一行设置颜色 - How to set color for last row in excel using pandas python 如何使用Python Win32com在Excel中查找最后一行是否具有特定列的值 - how to find if last row has value for a specific column in Excel using Python Win32com 如何使用元组和openpyxl对电子表格行中的最后一个元素求和? - how do you sum the last element in a spreadsheet row using tuples and openpyxl? 如何通过python在现有的Excel电子表格上放置列名? - How do you put column names on existing excel spreadsheet through python? 如何使用 python 找到 excel 中列中的数字之间的差异? - How do you find the difference between numbers in a column in excel using python? 您如何从Excel电子表格中获取函数并使用python运行它? - How do you take a function from an excel spreadsheet and run it with python? 如何在Python中使用@将列向量与行向量相乘? - How do you multiply a column vector with a row vector using @ in Python? 如何在 Pandas Data Frame 行中找到最后一个索引并使用列信息向后计数? - How to find last index in Pandas Data Frame row and count backwards using column information? 我如何使用 python 检索熊猫分组的最后一列行值? - How do i use python to retrieve the last column row value for a pandas group-by? 如何使用熊猫在python中编写Excel行 - How to write excel row in python using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM