如何使用python pandas在excel电子表格中找到最后一列 [英] how do you find the last column or row in an excel spreadsheet using python pandas
问题描述
在Excel VBA中,我可以通过编程方式确定excel电子表格中的列和行数,但是如何在python中确定?
col-1 | col -2
/ pre>
1.蓝色
2.绿色
3.蓝色
4.空白
5.空白
我需要一些python代码,最好是python - pandas方法的参数read_excel,它告诉我传递给它的文档中的列数为2,如果您包含标题,则行数为5或6。
但是,我需要这样做才可编程(称为每次),因为我想为其提供不同的电子表格
解决方案使用
fillna
,方法'ffill' / p>
在[71]中:
df1 = pd.DataFrame({'a':[ 2,3,NaN,NaN,4,5,6],'b':[1,2,NaN,NaN,3,NaN,4,5]})
df1
输出[71 ]:
ab
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行×2列]
在[85]中:
df1.fillna(method = 'ffill',inplace = True
df1
输出[85]:
ab
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行×2列]
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??
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.
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'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]:
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]
这篇关于如何使用python pandas在excel电子表格中找到最后一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!