使用Python从元组中获取2列
我有一个元组,当我遍历它的行时,它看起来像这样:使用Python从元组中获取2列,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个元组,当我遍历它的行时,它看起来像这样: for row in df.itertuples(index=False, name=None): print(row) o/p: 所需输出: ('120.6843686', '-41.9098438') ('121.7692179', '-42.2737880') ('122.6417215', '-43.8718865') 我是Python新手,因此非常感谢您的帮助。 谢谢。请使用以下代码: for row in df.i
for row in df.itertuples(index=False, name=None):
print(row)
o/p:
所需输出:
('120.6843686', '-41.9098438')
('121.7692179', '-42.2737880')
('122.6417215', '-43.8718865')
我是Python新手,因此非常感谢您的帮助。
谢谢。请使用以下代码:
for row in df.itertuples(index=False, name=None):
print(row[1:])
这将切片元组并显示列0之后的所有内容。如果您感兴趣,这将对其进行更详细的解释。如果您只是想获取值,这里有一个简单的方法:
import pandas as pd
df = pd.DataFrame((
(100214, '120.6843686', '-41.9098438'),
(101105, '121.7692179', '-42.2737880'),
(101847, '122.6417215', '-43.8718865'))
)
df = df.iloc[:, 1:].values.tolist()
print(df)
了解Python的切片语法<代码>行[1:] /代码>切片将给出这里需要的结果,但是您也可以考虑只通过引用特定列重复您的数据框的子集:“df:[COL1','COL2']中的行<代码>。
import pandas as pd
df = pd.DataFrame((
(100214, '120.6843686', '-41.9098438'),
(101105, '121.7692179', '-42.2737880'),
(101847, '122.6417215', '-43.8718865'))
)
df = df.iloc[:, 1:].values.tolist()
print(df)
[['120.6843686', '-41.9098438'],
['121.7692179', '-42.2737880'],
['122.6417215', '-43.8718865']]