[英]How to replace dataframe column values using a function
I'm working on a large CSV, where I need to map the IP Protocol Column from String to there IP Protocol Number.我正在研究一个大型 CSV,我需要 map IP 协议列从字符串到 ZA12A16AZ9579E8ACED24 号。 For example TCP -> 6.
例如 TCP -> 6。
I want to use this to map the values.我想用这个来 map 的值。
socket.getprotobyname("TCP")
My first option to map all the columns is to use a dictionary and then the replace function.我对 map 所有列的第一个选择是使用字典,然后替换 function。
ip_dict = {"TCP": socket.getprotobyname("TCP"), "UDP": socket.getprotobyname("UDP")ect...}
df.replace({"proto": ip_dict})
But I want to use the the socket function inside the replace function but I can't find a way to get the current value to pass it as argument但我想在替换 function 内部使用套接字 function 但我找不到获取当前值的方法将其作为参数传递
df["protocol"] = df["protocol"].str.replace(this*, socket.getprotobyname(this*))
this is the the current value to be replaced.这是要替换的当前值。
Thanks!谢谢!
You can use the apply
method to create a new column by apply a function to each element of a given column.您可以使用
apply
方法通过将 function 应用于给定列的每个元素来创建新列。
So in your case, you can do this:所以在你的情况下,你可以这样做:
df["protocol"] = df["protocol"].apply(socket.getprotobyname)
Equivalently, applying a lambda function makes it clear that you're passing every element as an argument (as you intended to do in your attempt):同样,应用 lambda function 可以清楚地表明您将每个元素作为参数传递(正如您在尝试中打算做的那样):
df["protocol"] = df["protocol"].apply(lambda this: socket.getprotobyname(this))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.