2017-02-20 108 views
2

首先,我對Python還是一個新手,並且已經搜索並且無法找到任何地方如何做到這一點(從一個新的角度來看)...遍歷一個數據框來打印索引和列和值

我有一個python

DataFrame

我需要打印索引,列名和值。

比方說,我有以下的數據幀

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']) 

哪位能給我的指數和值,但不會列再加上我希望它能夠不管迭代多少列或行使用。另外,我還需要能夠插入其需要是動態的文字...

回答

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 
+0

我曾經遇到過堆棧和interitems,但不確定讓我到哪裏需要的語法。完全按照我需要的方式工作。 – maddhatter1219

1

可以使用stack爲重塑到SeriesMultiIndex,然後通過Series.iteritemsformat迭代:

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添加DataFrame.iterrowsreset_index然後循環:

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 
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問題。

相關問題