【发布时间】:2019-02-02 15:49:45
【问题描述】:
import pandas as pd
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': [2, 3, 4]})
for i=0 to range(len(df))
print(df.iloc[i]['col1'] + '-' + df.iloc[i]['col2'])
我想使用:(但它给了我错误)
for d in df:
print(d['col1'] + '-' + d['col2'])
# I want output
# 2-3
# 3-4
仅限简单的 Pandas 问题...
【问题讨论】:
-
你能解释一下吗,你需要什么?
-
#我要输出#2-3#3-4
-
你可以把
for val in df['col1']:改成for val in df['col1'].astype(str) + ' - ' df['col2'].astype(str): -
谢谢!你说对了! .astype 和 val !!!你在读什么 Python 字典??!!
-
不,不是字典,类似
dict in pandas叫Series