“如何从数据帧的单个列中提取周期性数据”

2024-06-16 14:11:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个161941行×76列的大数据CSV文件,从中提取了161941行×3列的有用数据。你知道吗

现在我的数据框看起来是这样的

Extracted Dataframme of size 161941 rows × 3 columns

“bKLR\u Touchauswertung”列是周期性数据,以这种形式显示

"bKLR_Touchauswertung"
7
7
10
10
10
10
10
7
7
0
0
0
0
0
0
0
0
0
0
7
7
10
10
10
10
10
10
7
7
0
0
0
0
0
0
0
0
7
7
10
10
10
10
10
7
7
0
0
0
0
0
0

一直重复到最后

我想从中得到的是。你知道吗

列中的每一组非零值都应该作为新列出现在数据帧中。你知道吗

比方说,第一组非零值应该作为一个新列“set1”,以此类推。。你知道吗

如果我能找到任何可能的解决办法那就太好了。 谢谢, 阿比奈


以下是初始和预期数据帧的更详细示例:

下面是我的数据框

               temp     toucha
Timestamp      

**185            83         7
191            83         7
197            83         10
.              .          .
.              .          .
.              .          .
2051           83         10**

2057           83         0
2063           83         0
2057           83         0
.              .          .
.              .          .
.              .          .
3000           83         0

**3006           83         7
3012           83         7
3018           83         10
.              .          .
.              .          .
.              .          .
6000           83         10**

6006           83         0
6012           83         0
6018           83         0
.              .          .
.              .          .
.              .          .
8000           83         0

这个序列继续下去

现在,我需要一个像这样的数据帧

                temp     toucha  set1   set2    ste3.............
Timestamp      

**185            83         7     7      0
191            83         7      7      0
197            83         10     10     0 
.              .          .      .      .
.              .          .      .      .
.              .          .      .      .
2051           83         10     10     0**

2057           83         0      0      0
2063           83         0      0      0
2057           83         0      0      0
.              .          .      .      .
.              .          .      .      .
.              .          .      .      .
3000           83         0      0      0

**3006           83         7     0      7
3012           83         7      0      7
3018           83         10     0      10
.              .          .      .      .
.              .          .      .      .
.              .          .      .      .
6000           83         10     0      10**

6006           83         0      0      0
6012           83         0      0      0
6018           83         0      0      0
.              .          .      .      .
.              .          .      .      .
.              .          .      .      . 
8000           83         0      0      0

Tags: columns文件ofcsv数据sizetemptimestamp
2条回答
# use a simple consecutive index
df.reset_index(inplace=True)

# split the indices on transition between null and non null values
subs = np.split(df.index.values,
            df[((df.toucha == 0)&(df.toucha.shift() != 0)
                 |(df.toucha != 0)&(df.toucha.shift() == 0))
                ].index.values)

# process those sequences
for i, a in enumerate(subs):
    # ignore empty or 0 value sequences
    if len(a) == 0: continue
    if df.toucha[a[0]] == 0: continue
    df['set'+str(i)] = 0    # initialize a new column with 0
    df.loc[a, 'set'+str(i)] = df.toucha.loc[a]  # and copy values

# set the index back
df.set_index('Timestamp', inplace=True)

如果您可以接受setxx列的数目不一定是连续的,那么可以使用shift来检测0和非0值之间的更改,然后np.split分割这些更改的数据帧索引。你知道吗

一旦这样做了,就可以简单地为每个序列添加一个0的新列并复制其中的原始值。但由于np.split,使用简单的连续索引更容易。所以代码可能是:

# use a simple consecutive index
df.reset_index(inplace=True)

# split the indices on transition between null and non null values
subs = np.split(df.index.values,
                df[((df.toucha == 0)&(df.toucha.shift() != 0)
                     |(df.toucha != 0)&(df.toucha.shift() == 0))
                    ].index.values)

# process those sequences
for i, a in enumerate(subs):
    # ignore empty or 0 value sequences
    if len(a) == 0: continue
    if df.toucha[a[0]] == 0: continue
    df['set'+str(i)] = 0    # initialize a new column with 0
    df.loc[a, 'set'+str(i)] = df.toucha.loc[a]  # and copy values

# set the index back
df.set_index('Timestamp', inplace=True)

使用以下示例数据

           temp  toucha
Timestamp              
185          83       7
191          83       7
197          83      10
2051         83      10
2057         83       0
2063         83       0
2057         83       0
3000         83       0
3006         83       7
3012         83       7
3018         83      10
6000         83      10
6006         83       0
6012         83       0
6018         83       0
8000         83       0

它给出:

           temp  toucha  set0  set2
Timestamp                          
185          83       7     7     0
191          83       7     7     0
197          83      10    10     0
2051         83      10    10     0
2057         83       0     0     0
2063         83       0     0     0
2057         83       0     0     0
3000         83       0     0     0
3006         83       7     0     7
3012         83       7     0     7
3018         83      10     0    10
6000         83      10     0    10
6006         83       0     0     0
6012         83       0     0     0
6018         83       0     0     0
8000         83       0     0     0

相关问题 更多 >