Trying to use a value from a column in a dataframe (placeholder) to reference a specific column in the same dataframe... Wondering if this is possible. Examples of input and output below:
enter:
Output:
Any help would be greatly appreciated!
Attempt:
df["final"] = df.apply(lambda x: str(x[str(x["placeholder"])]) + "_id", axis=1) print(df)
Print:
id 1 2 3 placeholder final 0 9234 923 12 942 2 12_id 1 203841 1230 438 1029 1 1230_id 2 94532 4380 312 349 3 349_id
If the column's type is integer, remove the inner str()
:
df["final"] = df.apply(lambda x: str(x[x["placeholder"]]) + "_ID", axis=1)
The above is the detailed content of Using a string in one column of a dataframe to reference a value in another column. For more information, please follow other related articles on the PHP Chinese website!