0

i have the following dataframe:

            Date level_1   AdjClose      Close       High        Low       Open     Volume  stockID  ID
0     2020-09-11       A  97.983307  98.599998  99.620003  98.019997  98.709999  1368600.0        0   0
1     2020-09-11      AA  13.980000  13.980000  14.280000  13.675000  13.890000  4217300.0        0   0
2     2020-09-11     AAA  24.792126  25.035000  25.049999  25.020000  25.040001    33400.0        0   0
3     2020-09-11    AAAU  19.360001  19.360001  19.469999  19.320000  19.440001   367700.0        0   0
4     2020-09-11    AACG   1.060000   1.060000   1.090000   1.060000   1.080000    22700.0        0   0
...          ...     ...        ...        ...        ...        ...        ...        ...      ...  ..
20445 2021-09-10     ACP  11.350000  11.350000  11.520000  11.310000  11.430000   145600.0        0   0
20446 2021-09-10   ACQRU   9.860000   9.860000   9.870000   9.860000   9.870000     1100.0        0   0
20447 2021-09-10     ACR  15.850000  15.850000  16.020000  15.620000  15.810000    42400.0        0   0
20448 2021-09-10    ACRE  15.450000  15.450000  15.780000  15.430000  15.780000   309400.0        0   0
20449 2021-09-10    ACRO   9.650000   9.650000   9.660000   9.650000   9.660000      200.0        0   0

And i have a dict for assigning a symbol to an id - part of the dict looks like this:

{'A': 9774, 'AA': 9847, 'AAA': 9799, 'AAAU': 9816, 'AAC': 8331, 'AAC.U': 8568, 'AACG': 8731, 'AACIU': 8571}

Now i want to use this dict to update the stockID-column in the dataframe. (per row see the symbol in column level_1 and then put the id according to the dict in the column stockID for the row)

I tried it with this code - but this seems to be very slow...

for index, row in df.iterrows():

    row["stockID"] = stockDict[df.iloc[index]["level_1"]]
    df.iloc[index] = row

What is a good and fast way to achieve this?

Rapid1898
  • 895
  • 1
  • 10
  • 32
  • 1
    `df['stockId'] = df['level_1'].map(stockDict)` or `df['stockId'] = df['level_1'].replace(stockDict)` (depending on desired behaviour see the differences in the linked duplicate). – Henry Ecker Sep 11 '21 at 20:25
  • 1
    this `df['stockId'] = df['level_1'].map(stockDict)` works great form me - thanks – Rapid1898 Sep 12 '21 at 18:01

0 Answers0