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

I want to use: (but it is giving me error)

for d in df:
    print(d['col1'] + '-' + d['col2'])
# I want output
# 2-3
# 3-4

Simple Pandas question only...

  • 3
    Can you explain more, what do you need? – jezrael Aug 28 '18 at 05:25
  • # I want output # 2-3 # 3-4 –  Aug 28 '18 at 08:26
  • You can change `for val in df['col1']:` to `for val in df['col1'].astype(str) + ' - ' df['col2'].astype(str):` – jezrael Aug 28 '18 at 08:29
  • Thanks! you got it! .astype and val!!! What Python dictionary are you reading??!! –  Aug 28 '18 at 08:42
  • No, it is not dictionary, it is similar `dict in pandas` called `Series` – jezrael Aug 28 '18 at 08:44
  • My data has 5 rows and 2 columns, but the for loop read 10 values. How can I make it read 5 rows only? {0 1a3 1 2a4 2 3a5 3 4a6 4 5a7 dtype: object 0 1a3 1 2a4 2 3a5 3 4a6 4 5a7 dtype: object} –  Aug 28 '18 at 09:01
  • Please [edit] your question to provide a [mcve] showing what you've tried so far, the inputs and outputs, and what you wanted the outputs to be instead. – Kevin J. Chase Aug 28 '18 at 10:46
  • @KevinJ.Chase questions edited. Still open for an answer. –  Aug 29 '18 at 05:06

3 Answers3

2

If want loop by all columns:

for col in df:
    print(df[col])

What is same like:

for col in df.columns:
    print(df[col])

Or if need loop by values of column col1:

for val in df['col1']:
    print(val)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

IIUC:

You want to print each row of columns 'col1'

print(*df.col1, sep='\n')
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0
for index, row in df.iterrows():
     print(row['col1'], row['col2'])

Thanks to @Jezrael link I am able to change my code into more future-looking code instead of mine, a bit of old-school LOL

Please 'click' Check for the correct answer.

The Zip method also correct but it will limit the flexibility of the code. Like for example, I would can again my data to string.format in a different order. So iterrows is the way to do it. Although, I have finished my project with my old code. But I changed it anyway to the new one and correct way.

  • Yes, or `itertuples` what is a bit faster. – jezrael Aug 29 '18 at 05:21
  • But if check [this](https://stackoverflow.com/questions/24870953/does-iterrows-have-performance-issues/24871316#24871316) better is use some vectorized functions if exist ;) – jezrael Aug 29 '18 at 05:22
  • @user122289 While your code may correct as answer But Elaborating what your code does, It can improve the quality of your answer. Checkout the Article : [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – LuFFy Aug 29 '18 at 06:24
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Aug 29 '18 at 06:56
  • So this will remove my 'Asking Ban' in StackOverflow? –  Aug 29 '18 at 08:58