【问题标题】:Conditionally populating pandas dataframe results in empty DataFrame [duplicate]有条件地填充熊猫数据框会导致空数据框[重复]
【发布时间】:2019-07-09 23:47:07
【问题描述】:

我正在逐行读取一个大文本文件,在阅读时,我想应用 if 条件,其中需要读取某些代码并将这些代码附加到数据帧中。我有一个工作代码,它适用于 if tag = 'ABC-1234' 的 1 个代码,然后它可以工作,但是当我输入更多代码时,我最终得到空数据框。我有 100 多个代码,我现在只想阅读这些行。如果你们中的任何人提出更好的方法来处理我面临的问题,我将不胜感激。下面是工作代码示例。

import pandas as pd
filename ="C:/Users/abcd/Downloads/abcd-xyz-433.txt"
filename =filename
code= pd.read_excel('C:/Users/abcd/Downloads/xyz_codes.xlsx')
code_list=code['codes'].tolist()

with open(filename, 'r') as f:
    sample =[]
    for line in f:
        tag=line[:45].split('|')[5]
        if tag == 'AB-C711':                         #This works
            sample.append(line.split('|')) 

print('Everything in the list is read') 

我正在尝试使用两种不同的语句来使其发挥作用。但我最终得到了空的数据框。 Code_list 是从 excel 文件中的一列代码创建的列表。

if tag == ('AB-C711', 'AB-D702'):            #This doesnt work
            sample.append(line.split('|')) 

if tag == code_list:                         #This doesnt work
            sample.append(line.split('|'))  

我想逐行读取与我的代码列表匹配的文件,然后在分隔符上拆分数据并从中创建一个数据框。

【问题讨论】:

标签: python python-3.x if-statement text-files


【解决方案1】:
import pandas as pd
filename ="C:/Users/vgowda/Downloads/abcd-xyz-433.txt"
filename =filename
code= pd.read_excel('C:/Users/Downloads/abc_codes.xlsx')
code_list=code['codes'].tolist()

with open(filename, 'r') as f:
    sample =[]
    for line in f:
        tag=line[:45].split('|')[5]
        if tag in code_list:        # this works
#         if tag == 'KV-C901':
            sample.append(line.split('|')) 

print('arrays are appended and ready to create a dataframe out of an array') 

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2018-06-20
    • 2019-11-30
    • 2020-11-19
    • 1970-01-01
    相关资源
    最近更新 更多