Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将数据帧的一列的值复制到数据帧中其他数据帧的另一列?_Python_Python 3.x_Pandas_Python 2.7_Python Requests - Fatal编程技术网

Python 如何将数据帧的一列的值复制到数据帧中其他数据帧的另一列?

Python 如何将数据帧的一列的值复制到数据帧中其他数据帧的另一列?,python,python-3.x,pandas,python-2.7,python-requests,Python,Python 3.x,Pandas,Python 2.7,Python Requests,在将string列的值逐个复制到另一个dataframe的列时,我得到了一个包含方括号的输出: chk.at[index,'StartLocation1'] = chkn['StartLocation1'].values chk.at[index,'EndLocation1'] = chkn['EndLocation1'].values 0 [Petrol Pump-Ramji Ambedkar Nagar] 1 [V Ente

在将string列的值逐个复制到另一个dataframe的列时,我得到了一个包含方括号的输出:

chk.at[index,'StartLocation1'] = chkn['StartLocation1'].values
chk.at[index,'EndLocation1'] = chkn['EndLocation1'].values

0        [Petrol Pump-Ramji Ambedkar Nagar]
1                           [V Enterprises]
2                                   [Baola]
3                         [Dharmajyot-Vapi]
4    [KINGSTON TOWER VASAI Dominos-(THANE)]
Name: StartLocation1, dtype: object
所以我进一步想去掉这个[]括号: 我应用了这一点:

chk['EndLocation1'].str.strip('[]').astype(str)

0      nan
1      nan
2      nan
3      nan
4      nan
但是,我有很多价值观。请支持

看,这是我的全部代码:

chk['StartLocation1'] = ''
chk['EndLocation1'] = ''

for index, row in chk.iterrows():
    start = row.StartTime
    end = row.EndTime
    reg = row.RegistrationNo

    query = "SELECT TOP 1 RegistrationNo, GPSDateTime, Location  FROM GPSEventsDataCurrentWeek where GPSDateTime Between 'start_date' and 'end_date' and RegistrationNo = 'reg' and GroundSpeed > 0 ORDER BY GPSDateTime ASC"

    query = query.replace('start_date', start.strftime('%m/%d/%Y %H:%M:%S'))
    query = query.replace('end_date', end.strftime('%m/%d/%Y %H:%M:%S'))
    query = query.replace('reg', str(reg))

    chk1 = pd.read_sql(query, con=engine)
    

    chk1 = chk1.rename({'Location': 'StartLocation1','GPSDateTime': 'StartTime'}, axis=1)
   

    query2 = "SELECT TOP 1 RegistrationNo, GPSDateTime, Location  FROM GPSEventsDataCurrentWeek where GPSDateTime Between 'start_date' and 'end_date' and RegistrationNo = 'reg' and GroundSpeed > 0 ORDER BY GPSDateTime DESC"

    query2 = query2.replace('start_date', start.strftime('%m/%d/%Y %H:%M:%S'))
    query2 = query2.replace('end_date', end.strftime('%m/%d/%Y %H:%M:%S'))
    query2 = query2.replace('reg', str(reg))

    chk2 = pd.read_sql(query2, con=engine)
    

    chk2 = chk2.rename({'Location': 'EndLocation1','GPSDateTime': 'EndTime'}, axis=1)
  
    chkn = pd.merge(chk1,chk2, on = ['RegistrationNo'], how = 'outer')
    print(chkn[['StartLocation1','EndLocation1']])
    

    chk.at['StartLocation1'] = chkn['StartLocation1'].values
    chk.at[index,'EndLocation1'] = chkn['EndLocation1'].values
这是我的数据帧chk:

Companyid   RegistrationNo  Date    Hour    Value   RunningDuration StartTime   EndTime
0   236.0   MH-01-CJ-3571   2020-09-01  0.0 True    00:42:00    2020-09-01 00:08:00 2020-09-01 00:59:00
1   236.0   MH-01-CV-7460   2020-09-01  0.0 True    00:49:00    2020-09-01 00:09:00 2020-09-01 00:58:00
2   654.0   MH-04-JK-4102   2020-09-01  0.0 True    00:03:00    2020-09-01 00:11:00 2020-09-01 00:24:00
3   654.0   DN-09-R-9421    2020-09-01  0.0 True    00:02:00    2020-09-01 00:24:00 2020-09-01 00:54:00
4   236.0   MH-01-CV-7456   2020-09-01  0.0 True    00:04:00    2020-09-01 00:38:00 2020-09-01 00:42:00

也许一个可能的解决方案是通过连接列表中的元素()来选择列表中的唯一值。df.values返回一个n-array对象,因此返回的值是一个长度为1的列表,但建议改用df.to_numpy()


无论如何,我认为以这种方式分配列并不是最好的方法。

也许一个可能的解决方案是通过连接列表中的元素()来选择列表中的唯一值。df.values返回一个n-array对象,因此返回的值是一个长度为1的列表,但建议改用df.to_numpy()


无论如何,我认为以这种方式分配列并不是最好的方法。

原始数据帧是什么样子的?为什么不直接指定列呢
chk['StartLocation1']=chkn['StartLocation1']
为什么要逐个执行此操作?@nycorder我已经编辑了我的代码:你会明白为什么吗?@ScottBoston请检查编辑的代码!您的原始数据帧是什么样子的?为什么不直接指定列呢
chk['StartLocation1']=chkn['StartLocation1']
为什么要逐个执行此操作?@nycorder我已经编辑了我的代码:你会明白为什么吗?@ScottBoston请检查编辑的代码!