怎么用java转置csv,如何在csv表中进行行到列的数据转置?

I'm new to scripting. I have a table (Table1.txt) and I need to create another table that has Table1's rows arranged in columns and vice versa. I have found solutions to this problem for Perl and SQL but not for Python.

I just started learning Python two days ago, so this is as far as I got:

import csv

import sys

with open(sys.argv[1], "rt") as inputfile:

readinput = csv.reader(inputfile, delimiter='\t')

with open("output.csv", 'wt') as outputfile:

writer = csv.writer(outputfile, delimiter="\t")

for row in readinput:

values = [row[0], row[1], row[2], row[3]]

writer.writerow([values])

This just reproduces the columns as columns. What I would have liked to do now is to write the last line as writer.writecol([values]) but it seems that there is no command like that and I haven't found another way of writing rows as columns.

解决方案

The solution in general to transpose a sequence of iterables is: zip(*original_list)

sample input:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

program:

with open('in.txt') as f:

lis = [x.split() for x in f]

for x in zip(*lis):

for y in x:

print(y+'\t', end='')

print('\n')

output:

1 6 11

2 7 12

3 8 13

4 9 14

5 10 15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 PySpark 的 DataFrame API 来读取 CSV 文件,然后进行操作。具体实现过程如下: 1. 读取 CSV 文件,并将每 24 数据合并为一个 DataFrame。 ```python from pyspark.sql.functions import col, row_number from pyspark.sql.window import Window # 读取 CSV 文件 df = spark.read.format("csv").option("header", "true").load("path/to/csv") # 添加 df = df.withColumn("row_num", row_number().over(Window.orderBy(col("id")))) # 按照每 24 数据进行分组 grouped_df = df.groupBy(((col("row_num") - 1) / 24).cast("integer").alias("group_id")) # 合并每组的数据为一个 DataFrame merged_df = grouped_df.agg(*[collect_list(col_name).alias(col_name) for col_name in df.columns]) ``` 2. 对每个 DataFrame 进行操作。 ```python from pyspark.sql.functions import array, explode, col # 定义函数 def transpose_df(df): # 获取名 cols = df.columns # 将所有换为数组 arrays = [array(col(col_name)).alias(col_name) for col_name in cols] # 合并所有数组为一个数组 data = arrays[0] for arr in arrays[1:]: data = array(data, arr) # 将数组换为 rows = explode(data).alias("row") # 提取的值,并将名作为 transposed_df = rows.select([col("row")[i].alias(cols[i]) for i in range(len(cols))]) return transposed_df # 对每个 DataFrame 进行操作 transposed_dfs = [transpose_df(group) for group in merged_df.select("*").toPandas().to_dict(orient="records")] ``` 3. 将后的 DataFrame 合并为一个 DataFrame。 ```python # 将后的 DataFrame 合并为一个 DataFrame result_df = transposed_dfs[0] for df in transposed_dfs[1:]: result_df = result_df.union(df) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值