简体   繁体   English

Pandas [如何将 append 一个 csv 文件的所有列合并为一个]

[英]Pandas [How to append all columns of a csv file into one]

I have this problem with Pandas as from a 'csv' file with multiple columns I would like to generate a new 'csv' file with a single column containing all the values as in the example below:我对 Pandas 有这个问题,因为来自具有多列的“csv”文件我想生成一个新的“csv”文件,其中包含所有值,如下例所示:

From:从:

column 1, column 2, column 3第 1 栏、第 2 栏、第 3 栏

1, 2, 3, 1, 2, 3,

4, 5, 6 4、5、6

I would like to obtain:我想获得:

column 1第 1 列

1 1

2 2

3 3

4 4

5 5

6 6

Thank you all in advance谢谢大家

You can use ravel on DataFrame values :您可以在 DataFrame values上使用ravel

pd.DataFrame({'column': df.values.ravel()})

Output: Output:

   column
0       1
1       2
2       3
3       4
4       5
5       6

you can try this你可以试试这个

import pandas as pd

df = pd.DataFrame({"column1": [1, 4], "column2": [2, 5], "column3": [3, 6]})
print("df : \n", df)

reshaped_value = df.values.reshape(-1)
new_df = pd.DataFrame(columns=["column1"])

new_df["column1"] = reshaped_value
print("\nnew_df : \n", new_df)

output: output:

df : 
    column1  column2  column3
0        1        2        3
1        4        5        6

new_df : 
    column1
0        1
1        2
2        3
3        4
4        5
5        6

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

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