Home > Mobile >  extract only one column to csv instead of all the columns [closed]
extract only one column to csv instead of all the columns [closed]

Time:09-17

I have json data in one column (Id, subject, words) with 5000 rows. I split the data which shows the results in Python great, but when I extract to SCV it shows only one row of the column. Do you know why and how to fix?

It shows:

0
 
| 0         |
| --------  |
|  word1    |
|  word2    |
|  word3    |

I need all the columns not only 0

Also when I print df after df=pd.DataFrame(s) is shows only the first row. Why?


for row in res:
            result=( ''.join(row[0]))
#             print(result)
#             print("\n")
            p = Payload(result)  
            print(p.ID)
            print(p.subject)
            s=p.words.split("|")
            print(s)  
            df=pd.DataFrame(s)
            for id, col in enumerate(s):
                df.to_csv('test.csv', sep='\t', encoding='utf-8')  
                #out = [s for c in df]
                #print(out)
                #df_big = pd.concat(out, ignore_index=True, axis=1)
                print(id, col)
                #print(df)
# writer=pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
# df.to_excel(writer, sheet_name='welcome')
# writer.save()             

CodePudding user response:

I believe you wanted to extract each column to a csv file.

This will iterate through the columns you have in the dataframe and export them to csv as column_name.csv

for col in df.columns.tolist():
    df[col].to_csv(os.path.join(col   ".csv"))

CodePudding user response:

You're creating a new dataframe for each row. Instead you can create the dataframe once and append new rows to the dataframe (which is still not efficient - see below).

df = pd.DataFrame()

for row in res:
        result = ( ''.join(row[0]))
        p = Payload(result)  
        s = p.words.split("|")
        df.loc[len(df.index)] = s

df.to_csv('test.csv', sep='\t', encoding='utf-8')

Ideally, you should have the data in list of lists from which you should create the dataframe.

data = []

for row in res:
    result = ( ''.join(row[0]))
    p = Payload(result)  
    s = p.words.split("|")
    data.append(s)

df = pd.DataFrame(data)

CodePudding user response:

inventory = list(Inventory.objects.all())
header = list(Header.objects.all())
setup = list(Setup.objects.all())
revision = list(Revisions.objects.all())
spec = list(SpecificationDetails.objects.all())
review = list(Reviewers.objects.all())
iterates = inventory   header   setup   revision   spec   review

zip_file = list(zip(iterates))

import pandas as pd

df = pd.DataFrame(zip_file, columns = ['Inventory', 'Header','Setup','Revisoin','Spec','Review'])

context = {
        'df':  df }
return render(request, 'crud/dashboard.html', df)
  • Related