简体   繁体   English

连接列值 pandas dataframe

[英]concatenate column values pandas dataframe

Hello I have a dateframe1 of values and i want to transform it into a new dataframe2 by concatenating values of columns in the original dataframe1 ie您好,我有一个值的 dateframe1,我想通过连接原始 dataframe1 中的列值将其转换为新的 dataframe2,即

dataframe1
ProductName  Value otherValue
Product1      2     5
Product2      3     2
Product1      1     5
Product3      4     7
Product3      5     7
Product1      5     5
Product2      9     2

dataframe2
ProductName  Value     otherValue
Product1      2 1 5       5
Product2      3 9         2
Product3      4 5         7

You could groupby ProductName and aggregate using ' '.join on Value and first on otherValue :您可以按ProductName分组并在Value上使用' '.joinfirstotherValue上进行聚合:

result = df.assign().groupby('ProductName', as_index=False).agg({ 'Value' : lambda x : ' '.join(map(str, x)), 'otherValue' : 'first' } )

print(result)

Output Output

  ProductName  Value  otherValue
0    Product1  2 1 5           5
1    Product2    3 9           2
2    Product3    4 5           7

Note that this solution assumes the column Value is not a string, otherwise you can use ' '.join directly请注意,此解决方案假定列 Value 不是字符串,否则您可以直接使用' '.join

You can try with this in two lines.您可以分两行尝试。 first we need to turn the column Value into strings so we can perform join and operations and the second are all the operations to return the desired output:首先,我们需要将列Value转换为字符串,以便我们可以执行连接和操作,第二个是返回所需 output 的所有操作:

import pandas as pd
import numpy as np 
df = pd.DataFrame(data={'ProductName':['Product1','Product2','Product1','Product3','Product3','Product1','Product2'],'Value':[2,3,1,4,5,5,9],'otherValue':[5,2,5,7,7,5,2]})
df['Value'] = df['Value'].astype(str)
df = df.merge(df.groupby('ProductName',as_index=True)['Value'].apply(' '.join).reset_index(),how='left',left_on='ProductName',right_on='ProductName').drop('Value_x',axis=1).drop_duplicates().rename(columns={'Value_y':'Value'})

print(df) Output:打印(df)Output:

  ProductName  otherValue   Value
0    Product1           5   2 1 5
1    Product2           2     3 9
3    Product3           7     4 5

暂无
暂无

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

相关问题 将 Pandas DataFrame 中的列值与“NaN”值连接起来 - Concatenate column values in Pandas DataFrame with “NaN” values Pandas - 将列表的值连接到 dataframe 中的单个列中 - Pandas - Concatenate values of lists into a single column in dataframe 尝试连接 Pandas Dataframe 中的两个列值 - Trying to Concatenate two column values in a Pandas Dataframe 如何将多个列值连接到 Pandas dataframe 中的单个列中 - How to concatenate multiple column values into a single column in Pandas dataframe 连接 Pandas DataFrame 中的列值,用逗号替换“NaN”值 - Concatenate column values in Pandas DataFrame replacing “NaN” values with comma 如何连接/替换熊猫数据框列中的特定单词和值 - How to concatenate/replace specific words and a values in pandas dataframe column Groupby on pandas dataframe,并根据列中值的频率用逗号连接字符串 - Groupby on pandas dataframe and concatenate strings with comma based on the frequency of values in a column 连接 pandas DataFrame 中的列值,同时忽略 NaN - Concatenate column values in a pandas DataFrame while ignoring NaNs 数据框列的 Pandas 条件连接 - Pandas conditional concatenate of a dataframe column 如果 dataframe 列中的单元格在另一列中具有相同的对应值,如何将它们连接到列表中??[熊猫] - How to concatenate cells in dataframe column into lists if they have same corresponding values in another column??[Pandas]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM