简体   繁体   中英

How can you use a for loop to append an array in python?

I'm trying to append an array using specific characters in the first column of a data frame for each row of the data frame.

The following code works for just the first row:

indx = []
ldate = data.iat[0, 0]
year = ldate[0:4]
quarter = ldate[6]
myIndex = year + " Q" + quarter
indx.append(myIndex)

But when I try to use a for loop to do it for each row I get and error message:

indx = []
for i in range(0,193):
    ldate = data.iat[0, i]
    year = ldate[0:4]
    quarter = ldate[6]
    myIndex = year + " Q" + quarter
    indx.append(myIndex)

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: invalid index to scalar variable.

Why can't the for loop iterate?

You should use [i, 0] instead of [0, i] because numpy.array and pandas.DataFrame (like matrix in math) use [row,column] instead of `[column,row]

import pandas as pd

df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])

for i in range(df.shape[0]):
    print(df.iat[i, 0], df[i].to_list())

You could also use df.iterrow() (or df.iteritems() , df.itertuples() ) to work with rows without range() and i

import pandas as pd

df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])

for idx, row in df.iterrows():
    print(row[0], row.to_list())

But in DataFrame you can do many things without loop

import pandas as pd

df = pd.DataFrame([
    ['2019  1', 1,2,3],
    ['2019  2', 4,5,6],
    ['2019  3', 7,8,9]])

result = df[0].apply(lambda x: x[:4] + " Q" + x[6])

print(result.to_list())

Result

['2019 Q1', '2019 Q2', '2019 Q3']

Or directly to the same DataFrame

df['result'] = df[0].apply(lambda x: x[:4] + " Q" + x[6])

print(df)

Result

         0  1  2  3   result
0  2019  1  1  2  3  2019 Q1
1  2019  2  4  5  6  2019 Q2
2  2019  3  7  8  9  2019 Q3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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