python pandas处理excel表单整合,如何使用python pandas在excel电子表格中找到最后一列...

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值