Meems
Meems

Reputation: 101

Why is pandas only writing one data point to my csv from my variables?

I'm trying to write 9 data points to a csv. My output looks like this:

1991579
['NC_021181.2']
4843789
['NZ_CP010820.1']
4848754
['NZ_CP009756.1']
5227419
['NC_007530.2']
3018755
['NC_013205.1']
5190487
['NZ_LR134475.1']
5762608
['NZ_CP009909.1']
5598454
['NZ_CP050508.1']
4215606
['NC_000964.3']

I want each ID to accompany each number in my dataframe. My code at the moment only writes one row. Am I missing something?

work_dir = "/Users/
for path in glob.glob(os.path.join(work_dir, "*.gbff")):
    with io.open(path, mode="r", encoding="utf-8") as file:
        first_line = file.readline()
        matches = int(re.search(r"(\d+)", first_line[33:]).group(0))
        print(matches)
    name = os.path.basename(path).replace(".gbff", "")
    names_list = []
    names_list.append(name)
    print(names_list)

#output as file
df = pd.DataFrame({'Accession_ID':names_list, 'Genome_size': matches})
print(df)
df.to_csv(r'/Users/, index=False)

Thank you!!

Upvotes: 0

Views: 81

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54743

You are clearing out names_list on every loop. You should initialize that once, before the loop starts.

Upvotes: 2

Related Questions