简体   繁体   English

将 x 行合并为一行 CSV, Pandas

[英]Combine x rows to one row CSV, Pandas

I have a dataframe looking like that:我有一个 dataframe 看起来像这样:

  ID  val1 val2 ... val10
1 1   45   2
2 1   34   90 
3 1   90   9
4 1   10   10
...
5000

I would like to merge the val-columns combining 10 rows (1-10, 11-20...) to 1 row so that it looks like this (every 10 rows the ID Changes, I do not wanna sort the data, just combine 10 rows to 1 row):我想将 val-columns 合并 10 行(1-10、11-20 ...)到 1 行,这样它看起来像这样(每 10 行 ID 更改,我不想对数据进行排序,只是将 10 行合并为 1 行):

  ID  val1 val2 ... val100
1 1   45   2
2 2   10   60 
3 3   40   9
4 4   1   140
...
500

I am pretty new to pandas and I was only able to find an easy method to sort and then combine rows by having the same column value.我是 pandas 的新手,我只能找到一种简单的方法来排序,然后通过具有相同的列值来组合行。 The problem is, that I only want to combine the val-columns of 10 rows and sometimes in my date the same ID occurs again.问题是,我只想合并 10 行的 val 列,有时在我的约会中再次出现相同的 ID。

This should do:这应该做:

import numpy as np
import pandas as pd

# create your input dataframe
df = pd.DataFrame({"ID": range(1, 5001)})
for n in range(1, 11):
    df[f"val{n}"] = np.random.randint(low=0, high=140, size=(5000, 1))

# initialize the output dataframe with the correct size and column names
out_df = pd.DataFrame(np.zeros((500, 100)))
out_df.columns = [f"val{n}" for n in range(1, 101)]
out_df.index += 1
out_df.index.name = "ID"

# copy the values from df to out_df
for n in range(10):
    out_df.iloc[0:500, n * 10: (n + 1) * 10 ] = df.copy().iloc[500 * n: 500 * (n+ 1), 1:]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM