简体   繁体   English

Pandas 遍历一个数据帧,将行值和列值连接到一个关于特定列值的新数据帧中

[英]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.

相关问题 Pandas 遍历数据框列并连接包含列表的相应行值 - Pandas-iterate through a dataframe column and concatenate corresponding row values that contain a list 将Pandas DataFrame中的行值除以特定列 - Divide row values in a pandas DataFrame by a specific column 遍历pandas数据框,使用if语句检查每个列值,并将该列值传递到空df的首选列 - Iterate through a pandas dataframe, check each column value with an if statement and pass the column values to the prefered column of an empty df 遍历 pandas 数据框中的行并匹配列表字典中的值以创建新列 - Iterate through rows in pandas dataframe and match values in a dictionary of lists to create a new column 遍历并覆盖熊猫数据框中的特定值 - Iterate through and overwrite specific values in a pandas dataframe 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas 尝试遍历pandas数据框并在列中显示特定值 - Trying to loop through a pandas dataframe and display specific values in a column 根据条件复制 Pandas 数据框中的行并更改特定列的值 - Replicate row in Pandas dataframe based on condition and change values for a specific column 从值相对于前一列中的值减小的列开始,用零填充 dataframe 的每一行 - fill each row of a dataframe with zero starting from the column in which the values decreases with respect to the value in the previous column 遍历pandas数据框并将新值插入空列 - Iterating through a pandas dataframe and inserting new values into an empty column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM