[英]Pandas-iterate through a dataframe concatenating row values and column values into a new dataframe with respect to a specific column value
I have a data-frame of 32250 rows x 901 columns :我有一个 32250 行 x 901 列的数据框: I want to iterate throughout the row values of column 'TRAINSET' and concatenate respective row value of columns '1','2','3'...n and keep 'date' as the same for the concatenated field:我想遍历列 'TRAINSET' 的行值并连接列'1','2','3'...n 的相应行值,并保持 'date' 与连接字段相同:
for example例如
d= { 'TS': ['a', 'b', 'c'],
'date': [ 7, 6, 8 ],
'X': ['x', 'x', 'x'],
'Y': ['y', 'y', 'y']
}
, ie after operation, the resultant dataframe will look like this ,即操作后,结果数据帧将如下所示
d= { 'TS+1': ['ax','ay','bx','by','cx','cy'],
'date': [ 7, 7, 6, 6, 8, 8 ],
'X': ['x', 'x', 'x', 'x', 'x', 'x'],
'Y': ['y', 'y', 'y', 'y', 'y', 'y']
}
column x,y....n contains 32250 entries of the same value please check the image for actual data description x,y....n列包含 32250 个相同值的条目,请查看图像以获取实际数据描述
The first few values of the resultant table will be like结果表的前几个值将类似于
d= { 'TRAINSET':['TNST175TC101','TNST175TC102','TNST175TC103','TNST175TC104','TNST175TC105'],
'date':[ '2018-1-5','2018-1-5','2018-1-5','2018-1-5','2018-1-5'],
'1': ['TC101', 'TC101', 'TC101', 'TC101', 'TC101'],
'2': ['TC102', 'TC102', 'TC102', 'TC102', 'TC102']
}
Thanks in advance :)提前致谢 :)
Use concat
with add new values to TS
columns by DataFrame.assign
, then DataFrame.sort_index
and create default RangeIndex
by reset_index
:使用concat
与添加新值TS
按列DataFrame.assign
,然后DataFrame.sort_index
和创建默认RangeIndex
通过reset_index
:
df = pd.concat([df.assign(TS = df['TS'] + 'x'),
df.assign(TS = df['TS'] + 'y')]).sort_index().reset_index(drop=True)
print (df)
TS date X
0 ax 7 x
1 ay 7 x
2 bx 6 y
3 by 6 y
4 cx 8 z
5 cy 8 z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.