首先,我还是Python新手并且已经搜索过并且无法在任何地方找到如何做到这一点(从一个新人的角度来看)...
我有一个python
我需要打印出索引,列名和值。
我们说我有以下数据框
EAT DAILY WEEKLY YEARLY
Fruit
APPLE 2 5 200
ORANGE 1 3 100
BANANA 1 4 150
PEAR 0 1 40
我需要打印它,以便我得到类似下面的内容,以便迭代数据帧中的每一行。
Eat Apple Daily at least 2
Eat Apple Weekly at least 5
Eat Apple Yearly at least 200
Eat Orange Daily at least 1
Eat Orange Weekly at least 3
Eat Orange Yearly at least 100
..
...
....
我尝试了各种组合但仍在学习,所以任何帮助都表示赞赏。
到目前为止,我已经尝试了
for row in test.iterrows():
index, data = row
print index , (data['column1'])
print index , (data['column2'])
print index , (data['column3'])
这将给我索引和值,但不是列加上我希望能够迭代,无论使用了多少列或行。另外,我仍然需要能够插入需要动态的文本......
答案 0 :(得分:1)
您可以使用stack
重塑Series
MultiIndex
,然后使用format
Series.iteritems
进行迭代:
test = test.stack()
print (test)
Fruit EAT
APPLE DAILY 2
WEEKLY 5
YEARLY 200
ORANGE DAILY 1
WEEKLY 3
YEARLY 100
BANANA DAILY 1
WEEKLY 4
YEARLY 150
PEAR DAILY 0
WEEKLY 1
YEARLY 40
dtype: int64
for index, data in test.iteritems():
print (('Eat {} {} at least {}').format(index[0], index[1], data))
Eat APPLE DAILY at least 2
Eat APPLE WEEKLY at least 5
Eat APPLE YEARLY at least 200
Eat ORANGE DAILY at least 1
Eat ORANGE WEEKLY at least 3
Eat ORANGE YEARLY at least 100
Eat BANANA DAILY at least 1
Eat BANANA WEEKLY at least 4
Eat BANANA YEARLY at least 150
Eat PEAR DAILY at least 0
Eat PEAR WEEKLY at least 1
Eat PEAR YEARLY at least 40
但如果确实需要DataFrame
添加reset_index
然后按DataFrame.iterrows
循环:
test = test.stack().reset_index(name='VAL')
print (test)
Fruit EAT VAL
0 APPLE DAILY 2
1 APPLE WEEKLY 5
2 APPLE YEARLY 200
3 ORANGE DAILY 1
4 ORANGE WEEKLY 3
5 ORANGE YEARLY 100
6 BANANA DAILY 1
7 BANANA WEEKLY 4
8 BANANA YEARLY 150
9 PEAR DAILY 0
10 PEAR WEEKLY 1
11 PEAR YEARLY 40
for index, data in test.iterrows():
print (('Eat {} {} at least {}').format(data['Fruit'], data['EAT'], data['VAL']))
Eat APPLE DAILY at least 2
Eat APPLE WEEKLY at least 5
Eat APPLE YEARLY at least 200
Eat ORANGE DAILY at least 1
Eat ORANGE WEEKLY at least 3
Eat ORANGE YEARLY at least 100
Eat BANANA DAILY at least 1
Eat BANANA WEEKLY at least 4
Eat BANANA YEARLY at least 150
Eat PEAR DAILY at least 0
Eat PEAR WEEKLY at least 1
Eat PEAR YEARLY at least 40
答案 1 :(得分:1)
字符串系列
f = 'Eat {Fruit} {EAT} at least {value}'.format
df.stack().reset_index(name='value').apply(lambda x: f(**x), 1)
0 Eat APPLE DAILY at least 2
1 Eat APPLE WEEKLY at least 5
2 Eat APPLE YEARLY at least 200
3 Eat ORANGE DAILY at least 1
4 Eat ORANGE WEEKLY at least 3
5 Eat ORANGE YEARLY at least 100
6 Eat BANANA DAILY at least 1
7 Eat BANANA WEEKLY at least 4
8 Eat BANANA YEARLY at least 150
9 Eat PEAR DAILY at least 0
10 Eat PEAR WEEKLY at least 1
11 Eat PEAR YEARLY at least 40
dtype: object
打印
for idx, value in df.stack().iteritems():
print('Eat {0[0]} {0[1]} at least {1}'.format(idx, value))
Eat APPLE DAILY at least 2
Eat APPLE WEEKLY at least 5
Eat APPLE YEARLY at least 200
Eat ORANGE DAILY at least 1
Eat ORANGE WEEKLY at least 3
Eat ORANGE YEARLY at least 100
Eat BANANA DAILY at least 1
Eat BANANA WEEKLY at least 4
Eat BANANA YEARLY at least 150
Eat PEAR DAILY at least 0
Eat PEAR WEEKLY at least 1
Eat PEAR YEARLY at least 40
答案 2 :(得分:0)
甚至考虑使用pandas.DataFrame.to_string的非循环解决方案:
sdf = df.stack().reset_index(name='VALUE')
sdf['Output'] = sdf.apply(lambda row: "EAT {} {} at least {}".\
format(row['Fruit'], row['EAT'], row['VALUE']), axis=1)
# PRINT TO CONSOLE
print(sdf[['Output']].to_string(header=False, index=False, justify='left'))
# WRITE TO TEXT
with open('Output.txt', 'w') as f:
f.write(sdf[['Output']].to_string(header=False, index=False, justify='left'))
# EAT APPLE DAILY at least 2
# EAT APPLE WEEKLY at least 5
# EAT APPLE YEARLY at least 200
# EAT ORANGE DAILY at least 1
# EAT ORANGE WEEKLY at least 3
# EAT ORANGE YEARLY at least 100
# EAT BANANA DAILY at least 1
# EAT BANANA WEEKLY at least 4
# EAT BANANA YEARLY at least 150
# EAT PEAR DAILY at least 0
# EAT PEAR WEEKLY at least 1
# EAT PEAR YEARLY at least 40
您会注意到该方法目前存在reported bug的理由问题。当然,您可以通过字符串处理(strip()
,replace()
)进行补救,基本Python。