Home>

There are two datagrama, it is necessary to replace the values ​​in the first datamaphrame on the Idashniki from the second datamaphrame, if the values ​​are validated by columns

df1= pd.dataframe (
    {
        "col1": ["String1", "String2"],
        "col2": [np.nan, "string1"],
        "col3": ["string2", np.nan],
        "col1_name": [np.nan, np.nan],
        "col2_name": ["first_name", np.nan],
        "col3_name": [np.nan, "last_name"]
    }
)
df2= pd.dataframe (
    {
        "String": ["String1", "String2"],
        "name": ["first_name", "last_name"],
        "Desc": ["Description 102", "Description 103"],
        "ID_": [101, 202]
    }
)

tried to do so (each column):

df1.col1= df1.col1.apply (lambda x: df2 [df2 ["string]== x] .id_)

but it works not as I expect this (I don't really understand the stove so ...) I would like to see this ( df1.col1. ) case:

col1 |
101 |
202 |

Please explain why the answer is different and how can I fix?

PS: I understand that when calling df1.col1.apply (lambda x: df2 [df2 ["String"]== x] .id_) Returns the datamaphrame 2x2, but I do not understand why.

I will repeat my question: why do you put your data in the datagram? Why is this format that is chosen?

strawdog2021-06-08 11:46:44

@strawdog due to a convenient way to work with data, and this (the fact that in the question) is only part of PiPlay. In essence, the entrance is large JSON, according to which it is difficult to make and conduct various manipulations with objects. There was an option when the value in the cell was a dictionary, but as you said -it breaks the essence of Pandas

Nobody2021-06-08 11:52:56