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