如何创建一个包含列名和唯一值的新数据框?

huangapple go评论57阅读模式
英文:

How to make a new df with column name and unique values?

问题

以下是您要翻译的代码部分:

#Create empty df
df_unique = pd.DataFrame()
#Loop to take unique values from each column and append to df
for col in df:
    list = df(col).unique().tolist()
    df_unique.loc[len(df_unique)] = list

这部分的翻译如下:

#创建空的数据框
df_unique = pd.DataFrame()
#循环以获取每列的唯一值并添加到数据框
for col in df:
    list = df(col).unique().tolist()
    df_unique.loc[len(df_unique)] = list

请注意,代码中的 "list" 变量可能会引发命名冲突,因为它是Python中的内置函数和数据类型名称。您可能需要使用不同的变量名来避免潜在的问题。

英文:

I am attempting to create a new df that shows all columns and their unique values. I have this following code but I think I am referencing the column of the df in the loop wrong.

#Create empty df
df_unique = pd.DataFrame()
#Loop to take unique values from each column and append to df
for col in df:
    list = df(col).unique().tolist()
    df_unique.loc[len(df_unique)] = list

To visualize what I am hoping to achieve, I've included a before and after example below.

Before

ID     Name        Zip       Type
01     Bennett     10115     House
02     Sally       10119     Apt
03     Ben         11001     House
04     Bennett     10119     House

After

Column List_of_unique
ID     01,  02,  03,  04
Name   Bennett,  Sally,  Ben
Zip    10115,  10119,  11001
Type   House,  Apt

答案1

得分: 1

你可以使用以下代码:

>>> df.apply(np.unique)

ID               [1, 2, 3, 4]
Name    [Ben, Bennett, Sally]
Zip     [10115, 10119, 11001]
Type             [Apt, House]
dtype: object

# OR
>>> (df.apply(lambda x: ', '.join(x.unique().astype(str)))
       .rename_axis('Column').rename('List_of_unique').reset_index())

  Column       List_of_unique
0     ID           1, 2, 3, 4
1   Name  Bennett, Sally, Ben
2    Zip  10115, 10119, 11001
3   Type           House, Apt
英文:

You can use:

>>> df.apply(np.unique)

ID               [1, 2, 3, 4]
Name    [Ben, Bennett, Sally]
Zip     [10115, 10119, 11001]
Type             [Apt, House]
dtype: object

# OR
>>> (df.apply(lambda x: ', '.join(x.unique().astype(str)))
       .rename_axis('Column').rename('List_of_unique').reset_index())

  Column       List_of_unique
0     ID           1, 2, 3, 4
1   Name  Bennett, Sally, Ben
2    Zip  10115, 10119, 11001
3   Type           House, Apt

huangapple
  • 本文由 发表于 2023年2月13日 23:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定