Muhammad Waqas
Muhammad Waqas

Reputation: 23

How to convert list elements in column

import csv
import pandas as pd
imp=[]
feature1 = []
issued = []
used = []
with open("lmutil_lmstat.txt", "r") as input:
    f=open("lmutil_lmstat.txt","r")
    found = False
    for x in f.readlines():
        if ("Users" in x):
            found = True
            feature1.append(x.split(" ")[2][:-1])
            issued.append(x.split(" ")[6][:])
            used.append(x.split(" ")[12][:])
            #print(x)
    
data = pd.DataFrame({'Feature_Name': [feature1], 'Licesence_Issued': [issued], 'Licesence_Used':[used]})    
data_frame = pd.DataFrame(data)
with open('license_summery1.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(data_frame)

This is my code I am taking specific data from file and store it in the list. while creating data frame I am getting the output (1, 3) but I want to create a table with output (57,3) Please check the above code and give suggestions. Any help will be appreciated.

Upvotes: 0

Views: 34

Answers (1)

Nyquist
Nyquist

Reputation: 336

I have actually just answered the same question here: how can change numpy array to single value?

You can use explode on your dataFrame. If you have multiple values in every element, it will expand your rows with single elements.

data_frame = data_frame.apply(pd.Series.explode)

Upvotes: 1

Related Questions