简体   繁体   English

如何遍历数据框中的行,并为每一行剪切每 3 个值并垂直堆叠这些值?

[英]How can I iterate over rows in a dataframe, and for each row, cut every 3 values and stack the values vertically?

I have a huge data frame that have 100 rows and 126900 columns.我有一个巨大的数据框,它有 100 行和 126900 列。 Each row in this dataframe represents a road trip of a vehicle.此数据框中的每一行代表车辆的一次公路旅行。 The columns are repetitive like this: longitude, latitude, timestamp, longitude, latitude, timestamp, longitude, latitude, timestamp, etc. Each row shows the a sequence of instant locations of the vehicle in each second.这些列是这样重复的:经度、纬度、时间戳、经度、纬度、时间戳、经度、纬度、时间戳等。每一行显示车辆在每一秒内的一系列即时位置。 Now for each row (each trip), I want to kind of split the repetitive columns and stack the values vertically.现在对于每一行(每次旅行),我想拆分重复的列并垂直堆叠值。 So the transformed dataframe should be a 3-columns dataframe.所以转换后的数据框应该是一个 3 列的数据框。

I am sorry I am new to Stackoverflow and do not know how to insert table like this.对不起,我是 Stackoverflow 的新手,不知道如何插入这样的表格。 Please see the screenshot here请在此处查看屏幕截图

I am thinking to use a double for loop with iterrow() method like this:我正在考虑使用带有 iterrow() 方法的双 for 循环,如下所示:

for index, row in df.iterrows(): for i in range(len(df)):对于索引,df.iterrows() 中的行:对于范围内的 i(len(df)):

But I am stuck here and do not know what to do next.但我被困在这里,不知道下一步该怎么做。 And it seems that looping through rows/columns in a huge dataframe might be generally a bad practice.似乎在巨大的数据框中循环遍历行/列通常可能是一种不好的做法。 Can anyone help me on this issue please?有人可以帮我解决这个问题吗?

Try this:尝试这个:

new_df = pd.DataFrame()
for c in range (0, len(df.columns)-1, 3):
    new_df = pd.concat([new_df, df[df.columns[c:3]]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历pandas数据帧中的每一行,并将所有行值乘以同一数据帧中的行值之一 - Iterate over every row in pandas dataframe and multiply all row values by one of the row values in same dataframe 如何计算行的列值与 dataframe 中具有多个值的所有其他行的差异? 迭代每一行 - How to calculate the difference of a row's column values against all other rows with multiple values in a dataframe? Iterate for every row 如何将 dataframe 中的每一行与另一个 dataframe 中的每一行进行比较,并查看值之间的差异? - How can I compare each row from a dataframe against every row from another dataframe and see the difference between values? 如何迭代多列数据框中的每个列值? - How to iterate over each individual column values in multiple column dataframe? 如何遍历字典的值并将它们作为行添加到相应的数据框? - How to iterate over a dictionary's values and add these as rows to a corresponding dataframe? 如何在 dataframe 中在一列中的两个值之间迭代行? - How to iterate over rows in a dataframe between two values in one column? 如何遍历前几行以比较 Pandas DataFrame 中的值 - How to iterate over previous rows to compare values in a Pandas DataFrame 如何在 Pandas 中的 DataFrame 中迭代行和索引以过滤布尔值 - How to iterate over rows and index in a DataFrame in Pandas to filter bolean values 遍历行熊猫数据框并添加值 - Iterate over rows pandas dataframe and add values 使用值迭代 Dataframe 行 - iterate over Dataframe rows using values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM