繁体   English   中英

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

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

我有一个数据框,我想使用 for 循环来获取列值和该值的索引。

下面是 dataframe 我正在尝试从日期列中获取值

在此处输入图像描述

下面是我的代码。 我声明了一个计数变量来跟踪索引。 我的问题:是否有可能在 for 循环声明中,我可以在一行中获取列值及其索引?

此行for row in loadexpense_df["Date"]:中的行的含义,行是包含日期列中的值的变量。 可以改进for循环以获取值及其索引吗?

谢谢

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

这是一个可以帮助你的例子。

mylist = ["ball","cat","apple"]

for idx, val in enumerate(mylist):
    print ("index is {0} and value is {1}".format(idx, val))

iterrows 返回索引和该行表示为系列的行

for index, row in df.iterrows():

请参阅此处了解更多信息:

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

这就是ititems的用途:

for idx, val in loadexpense_df['Date'].items():
    pass

但是,您的代码可能在链索引方面存在一些问题。 例如:

loadexpense_df["Month"][count] = "Jul"

我认为您应该查看np.select.loc访问权限。 这有助于提高代码的可读性和性能。 例如:

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

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

您必须考虑“熊猫方式”,并且应尽可能将循环创建留给 pandas。 一个解决方案示例:

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