简体   繁体   English

Python - 如何将多个变量存储在 .csv 文件的一列中,然后将这些变量读入列表

[英]Python - how to store multiple variables in one column of a .csv file and then read those variables into a list

I think this issue is pretty basic, but I haven't seen it answered online.我认为这个问题很基本,但我还没有看到它在网上回答。 I'm using python and I have 'pandas' installed to make things easier.我正在使用 python,并且安装了“熊猫”以使事情变得更容易。 If there's a way to do it without 'pandas' that would be awesome too!如果有办法在没有“熊猫”的情况下做到这一点,那也太棒了! I'm coding a node connected map.我正在编写一个节点连接图。 I want to be able to take in some .csv file with a 'previous' and 'next' node list.我希望能够接收一些带有“上一个”和“下一个”节点列表的 .csv 文件。 I want this data to be then read by the program and stored in a list.我希望这些数据随后被程序读取并存储在一个列表中。 For example:例如:

.csv file: .csv 文件:

Name姓名 Previous以前的 Next下一个
Alpha Α one two一二 Three
Beta贝塔 four five
Charlie查理 six seven eight七八

what I want in my program:我在我的程序中想要什么:

alpha, [one, two], [three]
beta, [four], [five]
charlie, [six], [seven, eight]

I have heard about two ways to write multiple variables in one .csv column.我听说过两种在一个 .csv 列中写入多个变量的方法。 One way was placing a space in between the two values/variables: alpha,one two,three一种方法是在两个值/变量之间放置一个空格: alpha,one two,three

and another way I've heard to solve this is use " marks and separate with a comma: alpha,"one,two",three我听说解决这个问题的另一种方法是使用 " 标记并用逗号分隔: alpha,"one,two",three

Although I have heard about these answers before, I haven't been able to implement them.虽然我以前听说过这些答案,但我无法实现它们。 When reading the data in my program, it will assume that the space is part of the string or that the comma is part of the string.在我的程序中读取数据时,它会假设空格是字符串的一部分,或者逗号是字符串的一部分。

file = pd.read_csv("connections.csv")
previous_alpha = []
previous_alpha.append(file.previous[0])

So, instead of having a list of two strings [one, two] my program will have a list containing one string that looks like ["one,two"] or [one two]因此,我的程序将有一个包含一个字符串的列表,而不是一个包含两个字符串[one, two]的列表,该列表看起来像["one,two"][one two]

I can change the way the variables are structured in the .csv file or the code reading in the data.我可以更改 .csv 文件中变量的结构方式或读取数据的代码。 Thanks for all the help in advance!感谢您提前提供的所有帮助!

If you have this DataFrame:如果你有这个数据框:

      Name Previous         Next
0    Alpha  one two        Three
1     Beta     four         five
2  Charlie      six  seven eight

Then you can split the strings in required columns and save the CSV normally:然后您可以将字符串拆分为所需列并正常保存CSV:

df["Previous"] = df["Previous"].str.split()
df["Next"] = df["Next"].str.split()

print(df)
df.to_csv("data.csv", index=False)
      Name    Previous            Next
0    Alpha  [one, two]         [Three]
1     Beta      [four]          [five]
2  Charlie       [six]  [seven, eight]

To load the data back, you can use pd.read_csv with converters= parameter:要重新加载数据,您可以使用pd.read_csvconverters=参数:

from ast import literal_eval

df = pd.read_csv(
    "data.csv", converters={"Previous": literal_eval, "Next": literal_eval}
)
print(df)

Prints:印刷:

      Name    Previous            Next
0    Alpha  [one, two]         [Three]
1     Beta      [four]          [five]
2  Charlie       [six]  [seven, eight]

There are multiple ways of doing this.有多种方法可以做到这一点。 Each for a different way you start with CSV data.每个都以不同的方式从 CSV 数据开始。

First method will have the data in CSV as a single row with lists of things:第一种方法将 CSV 中的数据作为包含事物列表的单行:

Name,Previous,Next
Alpha,"One,Two",Three
Beta,Four,Five
Charlie,Six,"Seven,Eight"

Note the quotation around the lists.注意列表周围的引用。 We can use apply to change the values.我们可以使用apply来更改值。 The convert function will just split the string using , as the delimiter. convert 函数只会使用,作为分隔符来拆分字符串。

import pandas as pd
def convert(x):
    return x.split(',')

df = pd.read_csv('file.csv')
df['Previous'] = df['Previous'].apply(convert)
df['Next'] = df['Previous'].apply(convert)

Second, each row is repeated for Name with the values in CSV:其次,使用 CSV 中的值对Name重复每一行:

Name,Previous,Next
Alpha,One,Three
Alpha,Two,Three
Beta,Four,Five
Charlie,Six,Seven
Charlie,Six,Eight

We can you the agg function to aggregate.我们可以对你的agg函数进行聚合。 The convert function drops the duplicates and returns as a list. convert函数删除重复项并作为列表返回。

import pandas as pd
def convert(x):
    return x.drop_duplicates().to_list()

df = pd.read_csv('file.csv')
df = df.groupby('Name').agg({'Previous': convert, 'Next': convert})

The results should look like this:结果应如下所示:

           Previous            Next
Name                               
Alpha    [One, Two]         [Three]
Beta         [Four]          [Five]
Charlie       [Six]  [Seven, Eight]

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

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