[英]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.