简体   繁体   English

1 for循环语句如何同时获取值和索引

[英]How to get the value and index at the same time with 1 for loop statement

I have a data frame and I would want to use for loop to get the column values and the index of that value.我有一个数据框,我想使用 for 循环来获取列值和该值的索引。

Below is the dataframe and I am trying to get values from Date column下面是 dataframe 我正在尝试从日期列中获取值

在此处输入图像描述

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?我的问题:是否有可能在 for 循环声明中,我可以在一行中获取列值及其索引?

Meaning in this line for row in loadexpense_df["Date"]: , row is the variable containing the value in the date column.此行for row in loadexpense_df["Date"]:中的行的含义,行是包含日期列中的值的变量。 Can improve the for loop to get value and its index?可以改进for循环以获取值及其索引吗?

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

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

iterrows returns the index and the row with the row represented as a series iterrows 返回索引和该行表示为系列的行

for index, row in df.iterrows():

See here for more info:请参阅此处了解更多信息:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html

This is what iteritems is for:这就是ititems的用途:

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.我认为您应该查看np.select.loc访问权限。 That helps with code readability as well as performance.这有助于提高代码的可读性和性能。 For example:例如:

checkMonth = loadexpense_df['Date'].dt.month

loadexpense_df.loc[checkMonth==1, 'month'] = 'Jul'
...

You must think "pandas way", and the loop creation should be left to the pandas whenever possible.您必须考虑“熊猫方式”,并且应尽可能将循环创建留给 pandas。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM