简体   繁体   中英

How to extract values from x column in pandas df, where y column in df == list(i)

Apologies if something similar has been asked before.

I have a task where I need a function that is fed a list of unix times, and a pandas df.

The pandas df has a column for unix time, a column for latitude, and a column for longitude.

I need to extract the latitude from the df where the df unix time matches the unix time in my list I pass to the function.

So far I have:

`def nl_lat_array(pandas_df, unixtime_list):

lat = dict()

data = pandas_df




for x, row in data.iterrows():

        if data[data['DateTime_Unix']] == i in unixtime_list:
            lat[i] = data[data['Latitude']] 



v=list(lat.values())

nl_lat_array = np.array(v)

return nl_lat_array

This results in the following error:

KeyError: "None of [Float64Index([1585403852.468, 1585403852.518, 1585403852.568, 1585403852.618,\\n 1585403852.668, 1585403852.718, 1585403852.768, 1585403852.818,\\n 1585403852.868, 1585403852.918,\\n ...\\n 1585508348.524, 1585508348.574, 1585508348.624, 1585508348.674,\\n 1585508348.724, 1585508348.774, 1585508348.824, 1585508348.874,\\n 1585508348.924, 1585508348.974],\\n dtype='float64', length=2089945)] are in the [columns]"

However these values in the pandas array do exist in the list I am passing.

Any help would be greatly appreciated.

You can do something like this.

filtered_data = data[data['DateTime_Unix'].isin(unixtime_list)]
filtered_data['Latitude'].values()
import pandas as pd
data = pd.DataFrame([[1,4,7],[2,5,8],[3,6,9]])
data.columns = ['time', 'lat', 'long']
time_list = [1,2]
d = data[data['time'].isin(time_list)]['lat'].values
# [4, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM