Pandas DataFrame.apply:使用来自两列的数据创建新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44588039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas DataFrame.apply: create new column with data from two columns
提问by Ulu83
I have a DataFrame (df) like this:
我有一个像这样的数据帧(df):
PointID Time geojson
---- ---- ----
36F 2016-04-01T03:52:30 {'type': 'Point', 'coordinates': [3.961389, 43.123]}
36G 2016-04-01T03:52:50 {'type': 'Point', 'coordinates': [3.543234, 43.789]}
The geojson column contains data in geoJSON format (esentially, a Python dict).
geojson 列包含 geoJSON 格式的数据(本质上是一个 Python 字典)。
I want to create a new column in geoJSON format, which includes the time coordinate. In other words, I want to inject the time information into the geoJSON info.
我想以 geoJSON 格式创建一个新列,其中包括时间坐标。换句话说,我想将时间信息注入到 geoJSON 信息中。
For a single value, I can successfully do:
对于单个值,我可以成功执行以下操作:
oldjson = df.iloc[0]['geojson']
newjson = [df['coordinates'][0], df['coordinates'][1], df.iloc[0]['time'] ]
For a single parameter, I successfully used dataFrame.apply in combination with lambda (thanks to SO: related question
对于单个参数,我成功地将 dataFrame.apply 与 lambda 结合使用(感谢 SO:相关问题
But now, I have two parameters, and I want to use it on the whole DataFrame. As I am not confident with the .apply syntax and lambda, I do not know if this is even possible. I would like to do something like this:
但是现在,我有两个参数,我想在整个 DataFrame 上使用它。由于我对 .apply 语法和 lambda 没有信心,我不知道这是否可能。我想做这样的事情:
def inject_time(geojson, time):
"""
Injects Time dimension into geoJSON coordinates. Expects a dict in geojson POINT format.
"""
geojson['coordinates'] = [geojson['coordinates'][0], geojson['coordinates'][1], time]
return geojson
df["newcolumn"] = df["geojson"].apply(lambda x: inject_time(x, df['time'])))
...but that does not work, because the function would inject the whole series.
...但这不起作用,因为该函数会注入整个系列。
EDIT: I figured that the format of the timestamped geoJSON should be something like this:
编辑:我认为带时间戳的 geoJSON 的格式应该是这样的:
TimestampedGeoJson({
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[-70,-25],[-70,35],[70,35]],
},
"properties": {
"times": [1435708800000, 1435795200000, 1435881600000]
}
}
]
})
So the time element is in the properties element, but this does not change the problem much.
所以时间元素在属性元素中,但这并没有太大改变问题。
回答by jezrael
You need DataFrame.apply
with axis=1
for processing by rows:
您需要DataFrame.apply
与axis=1
按行处理:
df['new'] = df.apply(lambda x: inject_time(x['geojson'], x['Time']), axis=1)
#temporary display long string in column
with pd.option_context('display.max_colwidth', 100):
print (df['new'])
0 {'type': 'Point', 'coordinates': [3.961389, 43.123, '2016-04-01T03:52:30']}
1 {'type': 'Point', 'coordinates': [3.543234, 43.789, '2016-04-01T03:52:50']}
Name: new, dtype: object