I would like to use unique in Pandas, but I don't know what to do because I can only take it out in Dataframe.
The data (df)
being read by Pandasab
-0.2 -0.200
-0.2 -0.195
-0.2 -0.190
-0.2 -0.185
-0.2 -0.180
I want to retrieve the data regardless of the column name, so I'm retrieving it using df.iloc[:,[0]]
.I would like to use unique
for this extracted data.
However, in the case of df.iloc[:,[0]]
, I would like df.iloc[:,[0]].unique
, but AttributeError: 'DataFrame' object has no attribute 'unique'
does not work.What should I do?
There are several ways to remove the pandas.Series from the column
df['a']
df.a
df[df.columns[0]]
df.iloc[:,0])
df.iloc[:,[0]]
lists, so the result is DataFrame
df[df.columns[0]].unique()
# array([-0.2])
Your question may be a little confusing, but I'll answer how to get a unique array of elements from the data frame.
import pandas as pd
import numpy as np
df = pd.DataFrame([-0.2, -0.200], [-0.2, -0.195],
[-0.2, -0.190], [-0.2, -0.185],
[-0.2, -0.180]], columns=['a', 'b'])
u1=np.unique(df.iloc[:,[0]].values)#original example
u2 = np.unique(df.values)# all elements
print(u1); print(u2)
[-0.2]
[-0.2 -0.195 -0.19 -0.185 -0.18 ]
df.iloc[:,0].unique())
, but if you are concerned with the pandas.DataFrame
type, you can use the pd.DataFrame.apply()
method.However, in this case, the output is also of type pandas.DataFrame
.
print(df.iloc[:,[0]].apply(pd.Series.unique))
print(df.iloc[:, [1]].apply(pd.Series.unique))
# a
# 0 -0.2
#
# b
# 0 -0.200
# 1 -0.195
# 2 -0.190
# 3 -0.185
# 4 -0.180
© 2025 OneMinuteCode. All rights reserved.