## 如何计算python中每行值的列数？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (339)

```IndividualID Trip1 Trip2 Trip3 Trip4 Trip5 Trip6 Trip7 Trip8 Trip9
200100001    23    1     2     4     4      1    5     5     5
200100002    21    1     12    3     1      55   7     7
200100003    12    3     3     6     3
200100004    4
200100005    6     5     3     9     3      5    6
200100005    23    4     4     2     4      3    6     5  ```

```IndividualID Trip1  Trip2  Trip3  Trip4  Trip5  Trip6  Trip7  Trip8  Trip9 Chains
200100001     23     1      2      4      4      1     5       5     5      9
200100002     21     1      12     3      1      55    7       7            8
200100003     12     3      3      6      3                                 5
200100004     4                                                             1
200100005     6      5      3      9      3      5     6                    7
200100005     23     4      4      2      4      3     6       5            8```

### 2 个回答

```df.iloc[:, 1:].count(1)

0    9
1    8
2    5
3    1
4    7
5    8
dtype: int64```

`df.iloc[:, 1:].replace('', np.nan).count(1)`

```df.ne('').sum(1)-1
Out[287]:
0    9
1    8
2    5
3    1
4    7
5    8
dtype: int64```

```df.iloc[:,1:].T.info()
<class 'pandas.core.frame.DataFrame'>
Index: 9 entries, Trip1 to Trip9
Data columns (total 6 columns):
0    9 non-null float64
1    8 non-null float64
2    5 non-null float64
3    1 non-null float64
4    7 non-null float64
5    8 non-null float64
dtypes: float64(6)
memory usage: 504.0+ bytes```