如何遍历dataFrame中的value_counts?

问题描述

我有一个csv文件,其中包含带有温度值的x,y,z坐标数据。我遍历了值计数并为每个value_counts提取了dataFrame的行。

  1. 但是我只想提取其中value_counts中的值大于20的行。我有下面的代码,并且还附加了csv文件

我尝试过

a. df['x'].value_counts>20. This gives me only boolean value in values of value_counts
b. df['x'].value_counts().values>20. This gives me only an array of values greater than 20 with boolean values.

Currently I have,df['x'].value_counts()

-0.00000    101
 0.00131    101
-0.00131    101
 0.00262     89
-0.00262     89
           ... 
-0.06202      2
-0.03805      2
 0.06050      2
 0.06545      1
-0.06545      1
Name: x,Length: 1493,dtype: int64

  1. 一旦我遍历value_counts,我就基于x的每个value_counts中的最大临时值提取了dataFrame,提取的值列表将附加在“ c”列表中。但是此列表包含每一行的标头,因为它包含数据帧列表,所以我无法删除它。我该如何删除
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('slice0.03.csv')
df = df.round(decimals = 5)

a = []
c = []

for x in df['x'].value_counts().index:
  a.append(df[df['x'] == x])

for i in range(len(a)):
  c.append(a[i][a[i]['T'] == a[i]['T'].max()])

c[0:3]

[        x     y        z        T
 4635 -0.0  0.03  0.00131  33.9615,x     y        z        T
 4636  0.00131  0.03  0.00131  33.9394,x     y        z        T
 4632 -0.00131  0.03  0.00131  33.9418]

供参考的数据链接https://drive.google.com/file/d/10_0MbXcP5iuI8rKn0LCHumuBqjJuPYd0/view?usp=sharing

感谢和问候,

Sunag R A。

解决方法

  1. 首先,您需要获取value_counts> 20的值。

bigger = df['x'].value_counts() > 20 # Gives you a series with boolean values
filtered_values = df['x'].value_counts().loc[bigger].index # array of values that have a count>20

  1. 然后,您需要在df中找到与这些值匹配的行:
filtered_df = df[df['x'].isin(filtered_values)] #Boolean indexing of rows that have an 'x' value in our list

,

我仍然不确定。无需修改先前的代码,您可以在末尾添加以下代码段,以帮助您获得没有索引器的列表:

c_without_indexers=[]

for element in c:
    for row in element.iterrows():
        temp=[]
        for col in element.columns:
            temp.append(element.loc[row[0]][col])
        c_without_indexers.append(temp)

c_without_indexers将包含答案。