Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在csv中有多个列。如何使用python将行值与引用列匹配?_Python_Python 2.7_Csv_Pandas - Fatal编程技术网

我在csv中有多个列。如何使用python将行值与引用列匹配?

我在csv中有多个列。如何使用python将行值与引用列匹配?,python,python-2.7,csv,pandas,Python,Python 2.7,Csv,Pandas,我有一个包含367列的csv文件。第一列有15个唯一值,随后的每一列都有这15个值的子集。在一列中不会多次找到唯一值。每列都已排序。我如何让行对齐?我的最终目标是制作一个存在/不存在热图,但我需要先获得正确格式的数据矩阵,这是我正在努力解决的问题 以下是我拥有的数据类型的一个小示例: 1,2,1,2 2,3,2,5 3,4,3, 4,,5, 5,,, 我需要行与引用匹配,但保持在同一列中,如下所示: 1,,1, 2,2,2,2 3,3,3, 4,4,, 5,,5,5 我的想法是使用panda

我有一个包含367列的
csv
文件。第一列有15个唯一值,随后的每一列都有这15个值的子集。在一列中不会多次找到唯一值。每列都已排序。我如何让行对齐?我的最终目标是制作一个存在/不存在热图,但我需要先获得正确格式的数据矩阵,这是我正在努力解决的问题

以下是我拥有的数据类型的一个小示例:

1,2,1,2
2,3,2,5
3,4,3,
4,,5,
5,,,
我需要行与引用匹配,但保持在同一列中,如下所示:

1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5

我的想法是使用
pandas
库,但我不知道如何解决这个问题,因为我对使用python非常陌生。我正在使用python2.7。

因此,您的问题肯定可以通过
熊猫
解决:

代码:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
结果:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
它是如何工作的?:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
当第一次接触熊猫时,它可能会有点令人畏惧,即使是对一个非常了解蟒蛇的人来说也是如此,所以我将试着从中走出来。我鼓励你做你需要做的事情来克服学习曲线,因为
pandas
对于这种数据操作来说是非常强大的

将数据放入帧中:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
这第一段代码除了将示例数据放入
pandas.DataFrame
中之外,什么都不做。您的数据格式没有指定,所以我假设您可以将其放入一个帧中,或者如果您无法将其放入一个帧中,将在这里询问另一个问题,关于将数据放入帧中

import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')    

# set the first column as an index
df = df.set_index([0])
构建结果框架:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
从一个仅仅是索引的结果帧开始

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)
# add each column to the dataframe indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])
对于源数据中的每一列,请查看该值是否在索引中

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)
# add each column to the dataframe indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])
就这样,用三行代码,我们计算了结果

输出结果:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
现在遍历包含布尔值的每一行,并以所需格式输出值(如int)

这将首先输出索引值,然后对于每个
True
值,再次输出索引,对于
False
值,输出空字符串

附言:

# Create the sample data into a data frame
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO(u"""
    1,2,1,2
    2,3,2,5
    3,4,3,
    4,,5,
    5,,,"""), header=None, skip_blank_lines=1).fillna(0)
for column in df:
    df[column] = pd.to_numeric(df[column], downcast='integer')

# set the first column as an index
df = df.set_index([0])

# create a frame which we will build up 
results = pd.DataFrame(index=df.index)

# add each column to the datafarme indicating if the desired value is present
for col in df.columns:
    results[col] = df.index.isin(df[col])

# output the dataframe in the desired format
for idx, row in results.iterrows():
    result = '%s,%s' % (idx, ','.join(str(idx) if x else ''
                                      for x in row.values))
    print(result)
1,,1,
2,2,2,2
3,3,3,
4,4,,
5,,5,5
这里有很多人比我更擅长熊猫,但是因为你没有在你的问题上加上熊猫关键字,他们可能没有注意到这个问题。但这让我可以在他们注意到之前接受我的回答。对于格式良好的问题,
pandas
关键字覆盖得很好,因此我非常确信,如果这个答案不是最佳答案,其他人会过来改进它。因此,在将来,一定要用
pandas
标记您的问题,以获得最佳答案

另外,您提到您是新的python,所以我将插入一个插件,以确保您使用的是一个好的IDE。我使用它,而且它和其他优秀的IDE可以使在python中工作更加强大,所以我强烈推荐它们