使用whitesp获取df列的len()

2024-06-14 10:56:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下资料:

df
   Site In Site Out Transmission Commodity     parameter         value unit
0      Mid    North         hvac      Elec           eff  9.000000e-01     
1      Mid    North         hvac      Elec      inv-cost  1.650000e+06     
2      Mid    North         hvac      Elec      fix-cost  0.000000e+00     
3      Mid    North         hvac      Elec      var-cost  0.000000e+00     
4      Mid    North         hvac      Elec      inst-cap  0.000000e+00     
5      Mid    North         hvac      Elec        cap-lo  0.000000e+00     
6      Mid    North         hvac      Elec        cap-up  1.500000e+15     
7      Mid    North         hvac      Elec          wacc  7.000000e-02     
8      Mid    North         hvac      Elec  depreciation  4.000000e+01     
9      Mid    South         hvac      Elec           eff  9.000000e-01
...     

当我这样做的时候,它就起作用了:

len(df.Transmission)
54

我如何获得'Site In''Site Out'的len(),因为它们的名称中有空格,所以我找不到使用.column_name的方法???

老实说,有几种方法可以获得长度,但是在这种情况下有没有方法使用.column_name?你知道吗

以下操作无效:

len(df.Site In)
*** SyntaxError: invalid syntax
len(df.SiteIn)
*** AttributeError: 'DataFrame' object has no attribute 'SiteIn'
len(df.Site_In)
*** AttributeError: 'DataFrame' object has no attribute 'Site_In'
len(df.Site' 'In)
*** SyntaxError: invalid syntax

Tags: 方法indflensitecolumnoutcap
1条回答
网友
1楼 · 发布于 2024-06-14 10:56:23
  1. len(df['Site In'])len(df['Site Out'])
  2. 您可以通过分配df['Site Out'].column.valuesdf['Site In'].column.values来重命名这些列,这样您的方法就可以工作了。你知道吗
  3. 或者,您可以使用测向iloc(1) 对于df['Site In']或测向iloc(2) 对于df['Site Out']。你知道吗

也有很多方法,但这些是最常见的。你知道吗

相关问题 更多 >