英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论