简体   繁体   English

为什么我只能在 python 中调用这个 .CSV 文件的第一列?

[英]Why can I only call the first column from this .CSV file in python?

I am trying to call the second column from this .CSV file in python so that I can carry out some data analysis with it, but I am only able to call the first column from this file ("Time_abs/ps").我试图在 python 中调用这个 .CSV 文件的第二列,以便我可以用它进行一些数据分析,但我只能调用这个文件的第一列(“Time_abs/ps”)。 I have tried calling both the second and third columns ("Signal/nA" and "Ref.signal/nA") individually in the same way as when I successfully call the first column, but I am receiving the error below whenever I try to do so.我尝试以与成功调用第一列时相同的方式分别调用第二列和第三列(“Signal/nA”和“Ref.signal/nA”),但每当我尝试时都会收到以下错误这样做。 Does anyone know why this is happening and know how to work around this?有谁知道为什么会发生这种情况并知道如何解决这个问题?

Input:输入:

import pandas as pd
import io
 
df = pd.read_csv(io.BytesIO(uploaded['wf air.csv']))

print(df)
print(df['Time_abs/ps'])
print(df['Signal/nA'])

Output:输出:

      Time_abs/ps   Signal/nA   Ref.signal/nA
0         1150.00    0.000865        0.001763
1         1150.05   -0.000280        0.002449
2         1150.10   -0.002557       -0.003643
3         1150.15   -0.002189       -0.003354
4         1150.20    0.004161        0.002784
...           ...         ...             ...
1396      1219.80    1.437238        1.439107
1397      1219.85    1.284854        1.286108
1398      1219.90    0.933574        0.934550
1399      1219.95    0.524213        0.528238
1400      1220.00    0.117463        0.120416

[1401 rows x 3 columns]
0       1150.00
1       1150.05
2       1150.10
3       1150.15
4       1150.20
         ...   
1396    1219.80
1397    1219.85
1398    1219.90
1399    1219.95
1400    1220.00
Name: Time_abs/ps, Length: 1401, dtype: float64
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

4 frames
/usr/local/lib/python3.7/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

/usr/local/lib/python3.7/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Signal/nA'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-63-04bb76cf1fc2> in <module>()
      6 print(df)
      7 print(df['Time_abs/ps'])
----> 8 print(df['Signal/nA'])

/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in __getitem__(self, key)
   3456             if self.columns.nlevels > 1:
   3457                 return self._getitem_multilevel(key)
-> 3458             indexer = self.columns.get_loc(key)
   3459             if is_integer(indexer):
   3460                 indexer = [indexer]

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'Signal/nA'

You can access the first column by index using df.iloc() function.您可以使用df.iloc()函数按索引访问第一列。

Syntax to select column with index at position 0.用于选择索引在位置 0 的列的语法。

df.iloc[:, 0]

The columns in a data frame can be referenced by name (eg df['Time_abs/ps']), so possible that the CSV contents have whitespace before or after the column name in the first row.数据框中的列可以按名称引用(例如 df['Time_abs/ps']),因此 CSV 内容可能在第一行的列名之前或之后有空格。 Dump the column names to verify they are as you expect them to be.转储列名以验证它们是否符合您的预期。

import pandas as pd
from io import StringIO

data = """Time_abs/ps,Signal/nA,Ref.signal/nA
1150.00,0.000865,0.001763
1150.05,-0.000280,0.002449
1150.10,-0.002557,-0.003643
1150.15,-0.002189,-0.003354
1150.20,0.004161,0.00278"""
 
df = pd.read_csv(StringIO(data))

# print(df)

# select column using name which returns a series
# print(df['Time_abs/ps'])
# print(df['Signal/nA'])

# select column with index position 0
col = df.iloc[:, 0]
print(col)

Output:输出:

0    1150.00
1    1150.05
2    1150.10
3    1150.15
4    1150.20
Name: Time_abs/ps, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM