简体   繁体   English

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

[英]how to copy values of one column of a dataframe to another column of other dataframe in pandas?

while copying one by one values of string column to another dataframe's column I got this as an output containing square brackets:在将字符串列的值一一复制到另一个数据框的列时,我将其作为包含方括号的输出:

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

So further I thought to remove this [] bracket: I have applied this:所以我想删除这个 [] 括号:我已经应用了这个:

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

0      nan
1      nan
2      nan
3      nan
4      nan

But, I have got nan values.但是,我有 nan 值。 Please support!请支持!

See this is my whole code:看到这是我的全部代码:

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

and this is my dataframe chk:这是我的数据框 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

Maybe a possible solution could be select the unique value in the list throught a join of the elements of lists ( join list of lists in python ).也许一个可能的解决方案是通过列表元素的连接选择列表中的唯一值( python 中的列表连接列表)。 df.values returns an n-array object, thus the returned values are a list of length 1, but is recommended used df.to_numpy instead ( https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.values.html ) df.values 返回一个 n 数组对象,因此返回的值是一个长度为 1 的列表,但建议使用 df.to_numpy 代替( https://pandas.pydata.org/pandas-docs/stable/reference/api/ pandas.DataFrame.values.html )

Anyway, I think that assigning the columns that way is not the best way to do it.无论如何,我认为以这种方式分配列并不是最好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果索引值相同,如何将一个DataFrame列复制到另一个Dataframe中 - How to copy one DataFrame column in to another Dataframe if their indexes values are the same 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas 将值从一个 dataframe 列复制到另一列 - Copy values from one dataframe column to another 如何将一列中的值传播到其他列中的行(熊猫数据框) - How to propagate values in one column to rows in other columns (pandas dataframe) pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe 根据另一列熊猫数据框的值更改一列的值 - Change values of one column based on values of other column pandas dataframe 如何将一个熊猫数据框的一列与另一个数据框的每一列相加? - How to sum a column of one pandas dataframe to each column of another dataframe? "Pandas:通过另一个数据帧的多个同时列值过滤一个数据帧" - Pandas: filter one dataframe by multiple, simultaneous column values of another dataframe 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas Pandas:使用其他数据框列映射一列值 - Pandas : Mapping one column values using other dataframe column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM