繁体   English   中英

如何遍历具有已排序数字索引的数据框唯一行的列值,并在熊猫中进行重复?

[英]How to iterate over column values for unique rows of a data frame with sorted, numerical index with duplicates in pandas?

我有一个带有重复的排序数字索引的pandas DataFrame ,对于给定列中相同索引值,列值相同。 我想遍历给定列的值以获取索引的唯一值。

df = pd.DataFrame({'a': [3, 3, 5], 'b': [4, 6, 8]}, index=[1, 1, 2])

   a  b
1  3  4
1  3  6
2  5  8

我想遍历索引a [3,5]唯一条目的a列中的值。

当我使用默认index进行迭代并打印a列的类型时,我得到了重复索引条目的Series条目。

for i in df.index:
    cell_value = df['a'].loc[i]
    print(type(cell_value))

输出:

<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'numpy.int64'>

首先通过面罩删除重复的指标,并指定由位置arange ,然后选择iloc

arr = np.arange(len(df.index))
a = arr[~df.index.duplicated()]
print (a)
[0 2]

for i in a:
    cell_value = df['a'].iloc[i]
    print(type(cell_value))

<class 'numpy.int64'>
<class 'numpy.int64'>

无循环解决方案-将boolean indexing~duplicated和反转掩码一起使用:

a = df.loc[~df.index.duplicated(), 'a']
print (a)
1    3
2    5
Name: a, dtype: int64

b = df.loc[~df.index.duplicated(), 'a'].tolist()
print (b)
[3, 5]

print (~df.index.duplicated())
[ True False  True]

试试np.unique

_, i = np.unique(df.index, return_index=True)
df.iloc[i, df.columns.get_loc('a')].tolist() 

[3, 5]

如果按照您的评论,如果相同的索引表示相同的数据,则这似乎是XY问题

您也不需要为此循环。

假设您要删除重复的行并仅提取第一列(即3、5),则下面的内容就足够了。

res = df.drop_duplicates().loc[:, 'a']

# 1    3
# 2    5
# Name: a, dtype: int64

要返回类型:

types = list(map(type, res))

print(types)
# [<class 'numpy.int64'>, <class 'numpy.int64'>]

另一种使用groupby的解决方案并应用:

df.groupby(level=0).apply(lambda x: type(x.a.iloc[0]))
Out[330]: 
1    <class 'numpy.int64'>
2    <class 'numpy.int64'>
dtype: object

为了使您的循环解决方案能够正常工作,请创建一个临时df:

df_new = df.groupby(level=0).first()
for i in df_new.index:
    cell_value = df_new['a'].loc[i]
    print(type(cell_value))

<class 'numpy.int64'>
<class 'numpy.int64'>

或使用drop_duplicates()

for i in df.drop_duplicates().index:
    cell_value = df.drop_duplicates()['a'].loc[i]
    print(type(cell_value))

<class 'numpy.int64'>
<class 'numpy.int64'>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM