Reputation: 301
I have a data frame and I would want to use for loop to get the column values and the index of that value.
Below is the dataframe and I am trying to get values from Date column
Below is my code. I declared a count variable to track the index. My question: Is it possible wherein the for loop declaration, I can get the column value and its index in one line?
Meaning in this line for row in loadexpense_df["Date"]:
, row is the variable containing the value in the date column. Can improve the for loop to get value and its index?
Thanks
count =0
load = loadexpense_df["Date"]
for row in loadexpense_df["Date"]:
checkMonth = row.strftime("%m")
if checkMonth == '01':
loadexpense_df["Month"][count] = "Jul"
elif checkMonth == '02':
loadexpense_df["Month"][count] = "Aug"
elif checkMonth == '03':
loadexpense_df["Month"][count] = "Sep"
elif checkMonth == '04':
count = count +1
Upvotes: 0
Views: 1085
Reputation: 2407
You must think "pandas way", and the loop creation should be left to the pandas whenever possible. A solution example:
df
Date Amount
0 2019-10-25 2
1 2019-10-26 5
2 2019-10-27 52
3 2019-10-28 93
4 2019-10-29 70
5 2019-10-30 51
6 2019-10-31 80
7 2019-11-01 61
8 2019-11-02 52
9 2019-11-03 61
m={10:'jul',11:'aug'}
# The easy way to get the month: dt.month
#df["Month"]= pd.Series.replace(df.Date.dt.month, m)
df["Month"]= df.Date.dt.month.replace(m)
Date Amount Month
0 2019-10-25 2 jul
1 2019-10-26 5 jul
2 2019-10-27 52 jul
3 2019-10-28 93 jul
4 2019-10-29 70 jul
5 2019-10-30 51 jul
6 2019-10-31 80 jul
7 2019-11-01 61 aug
8 2019-11-02 52 aug
9 2019-11-03 61 aug
Upvotes: 1
Reputation: 150735
This is what iteritems is for:
for idx, val in loadexpense_df['Date'].items():
pass
However, your code might have some problem with chain indexing. For example:
loadexpense_df["Month"][count] = "Jul"
I think you should look at np.select
, or .loc
access. That helps with code readability as well as performance. For example:
checkMonth = loadexpense_df['Date'].dt.month
loadexpense_df.loc[checkMonth==1, 'month'] = 'Jul'
...
Upvotes: 2
Reputation: 4094
iterrows returns the index and the row with the row represented as a series
for index, row in df.iterrows():
See here for more info:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html
Upvotes: 1
Reputation: 1166
Here's an example that could help you.
mylist = ["ball","cat","apple"]
for idx, val in enumerate(mylist):
print ("index is {0} and value is {1}".format(idx, val))
Upvotes: 1