![](/img/trans.png)
[英]how to extract the digits from the center of a column of data in a dataframe in pandas?
[英]“How to extract periodical data from a single column of the Pandas Dataframe”
我有一個161941行×76列的大數據CSV文件,其中我提取了161941行×3列的有用數據。
現在我的數據框看起來像這樣
列“ bKLR_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
如果您可以接受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
# 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.