简体   繁体   English

如何将结果从一个变量传输到excel中的列?

[英]How can i transfer the results from one variable to a column in excel?

I want to add the values inside duplicates to column Name so that print(data["Name"]) can return all the values contained by the duplicates . 我想将duplicates内的值添加到列Name以便print(data["Name"])可以返回duplicates包含的所有值。 How can I achieve this? 我怎样才能做到这一点?

Quick story: I'm importing a csv file and I need to split the column Name to get rid of meaningless information and then I'm using list comprehension to find the duplicates. 快速故事:我正在导入一个csv文件,我需要拆分列Name以删除无意义的信息,然后我使用列表Name来查找重复项。

data = pd.read_csv(next(iglob('*.csv')))
data["Name"]= data["Name"].str.split("(", n = 1, expand = True) 
duplicates = [x for x in data["Name"]  if x in data["Name"] 
[data["Name"].duplicated()].values]

Edit: 编辑:

df['dupicates'] = df['Name'].where(df['Name'].duplicated(keep=False), '')

    Name duplicates
0  NameC           
1  NameA      NameA
2  NameB      NameB
3  NameA      NameA
4  NameA      NameA
5  NameB      NameB

Or if you only want to label those duplicate values...(remove keep=False ) 或者,如果您只想标记这些重复值...(remove keep=False

df['duplicates'] = df['Name'].where(df['Name'].duplicated(), '')

    Name duplicates
0  NameC           
1  NameA           
2  NameB           
3  NameA      NameA
4  NameA      NameA
5  NameB      NameB

IIUC, you can try something like this: IIUC,您可以尝试这样的事情:

df = pd.DataFrame({'Name':['NameC', 'NameA', 'NameB', 'NameA', 'NameA', 'NameB']})
duplicates = df.loc[df['Name'].duplicated(), 'Name'].unique().tolist()
duplicates

Output: 输出:

['NameA', 'NameB']

Explanation: Use duplicates to create a boolean series, then filter the dataframe by the boolean series and column 'Name' then use unique to get the unique values of all the duplicates. 说明:使用duplicates创建布尔系列,然后通过布尔系列和“名称”列过滤数据框,然后使用唯一来获取所有重复项的唯一值。

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

相关问题 如何将参数从一个 gpflow model 传输到另一个以获得类似的结果? - How can I transfer parameters from one gpflow model to another to gain similar results? 如何使用 Python 脚本将指针变量从一个 cython 模块传输到另一个模块 - How can I transfer a pointer variable from one cython module to another using a Python script 如何将 Pyomo 变量的结果导出到 Pandas 数据框和 excel? - How can I export results from a Pyomo variable to a Pandas dataframe and excel? 如何将变量从 Python Flask 转移到 JavaScript? - How can I transfer a variable from Python Flask to JavaScript? 如何将数据从一个函数传输到另一个函数? - How can I transfer data from one funtction to another? 我如何按三列进行分组和计数并将结果传输到一个数据帧中 - How can i group by and count on three columns and transfer the results in one data frame 如何根据“isin”的结果将一个数据框中的列合并到另一个数据框中? - How can I combine a column from one dataframe into another based on the results of “isin”? 如何将 excel 列写入文本文件的一列? - How can i write excel columns into one column in text file? 如何将值从一列传输到另一列 - How to transfer values from one column to another column 使用python品脱如何将单位从一个变量转移到另一个 - using python pint how do I transfer a unit from one variable to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM