Skip to content
Advertisement

How to change column value with pandas .apply() method

I stumbled upon an issue while trying to take data from a CSV file, assemble a key and then creating a new CSV file with only the necessary data.

Example data:

ID1       Data1   Data2  Price1 Color   Key          ID2       Data3   Price2
12345/6  950/000  Pd950   996    G    4/20017/6   4/20017/6   950/000   1108
12345/6  333/000  Pd333   402    G    4/20017/6   4/20017/6   333/000    501
12345/6  500/000  Pd500   550    G    4/20017/6   4/20017/6   500/000    577
12345/6  950/000  PT950   690    Pt   4/20017/6   4/20017/6   950/000    779
12345/6  600/000  Pt600   424    Pt   4/20017/6   4/20017/6   600/000    482

My code:

JavaScript

The intended result would be a CSV file with just two columns “idNum” and “price” and the columns are limited as intended, however the rows all end up with the empty data I use to create these two new columns, so an empty string and the number 0.

After searching through Google I discovered that using .apply() does not allow you to directly change the data, instead it needs to be reassigned but when I change the respective line, I get an error.

JavaScript

The error reads as follows:

Traceback (most recent call last):
  File "prepareImport.py", line 278, in 
    file_processed = file_original[["idNum", "price"]]
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreseries.py", line 910, in __getitem__
    return self._get_with(key)
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreseries.py", line 958, in _get_with
    return self.loc[key]
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreindexing.py", line 1768, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreindexing.py", line 1954, in _getitem_axis
    return self._getitem_iterable(key, axis=axis)
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreindexing.py", line 1595, in _getitem_iterable
    keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreindexing.py", line 1552, in _get_listlike_indexer
    self._validate_read_indexer(
  File "C:UsersMY-USERanaconda3libsite-packagespandascoreindexing.py", line 1640, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['idNum', 'price'], dtype='object')] are in the [index]"
JavaScript

As far as I can tell the dataframe somehow looses the two columns I add before reassigning it.
So I decided to print it to the console rather than trying to extract two columns, the result shows that apparently the dataframe was transformed to a Series, so it has just one column filled with “None” of the datatype “object” although it keeps the full length of 10550.

I found a few other possible solutions to my original issue but they usually addressed an error the user made. Of course I checked my code for similar errors but couldn’t find any myself so I hope someone can help me out with this.

Thank you very much in advance! If I forgot to provide any kind of crucial information please let me know so I can add it.

Best regards
Eisman333

Edit: Expected output:

idNum                              price
4-12345-6_12345-6_XX-333            250
4-12345-6_12345-6_XX-585_YY-950    2130
4-12345-6_12345-6_XX-333_ZZ-500    1560

Advertisement

Answer

There is not enough of your logic/program for me to duplicate all the aspects of your processItems function, but I would suggest you think about separating out the individual column updates. You could implement something along the lines of the following where this method is called with the original dataframe:

JavaScript

This would require to separate the logic for identifying the alloys and materials into two passes, I suggest that, since you need to access both materials and alloys when generating the IdNum column later. Once you have all the data in the original dataframe, you can then create a new output frame containing just the information you need for the final result.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement