1

I would like to display the output of a query in Neo4j using the pandas data frame and write the result in a text file.

I can display the query but the output is always containing dots.

from py2neo import Graph
import pandas as pd
graph = Graph(host="63.35.194.218", auth=("neo4j", "neo4j"))
bidirectional_different_SAB = graph.run('MATCH (n1)-[r1:HAS_CHILD|HAS_DESCENDANT]->(n2)-[r2:HAS_CHILD|HAS_DESCENDANT]->(n1) WHERE r1.weight = r2.weight and id(n1) > id(n2) and r1.SAB<>r2.SAB RETURN  n1.prefered_name,r1.SAB, n2.prefered_name,r2.SAB, n1.id, n2.id;')
bidirectional_different_SAB = str(bidirectional_different_SAB.to_data_frame())
f= open("analysiss.txt","w+")
f.write("Display bidirectional relationships having different SAB")
f.write(bidirectional_different_SAB)
f.write("\n")

I would like to have all the data displayed in the text file with no dots This is the result shown, I need the result without dots

Stetco Oana
  • 101
  • 1
  • 1
  • 11

2 Answers2

3

Try using the pandas data frame API to write the file directly.

df = bidirectional_different_SAB.to_data_frame()
df.to_csv("Display bidirectional relationships having different SAB.csv")

If you want the repr format, you can modify pandas options using set_option before you run the rest of your code.

import pandas as pd

pd.set_option('display.width', 30000)
pd.set_option('display.max_columns', 1000)
pd.set_option('display.max_colwidth', 1000)

As a third alternative, you can convert the data frame to string, pad it to fixed width per column, and then save the date frame to file using the pandas API.

import pandas as pd

def get_colwidth(col):
    w_header = len(col.name) if col.name else 0
    w_col = col.astype(str).str.len().max()
    return max(w_header, w_col)

def to_fixed_width(df):
    df_out = df.astype(str)
    for c in df_out:
        col = df_out[c]
        width = get_width(col)
        df_out[c] = col
    # convert the index to str as well
    ix = df_out.index
    df_out = df_out.set_index(ix.str.rjust(get_width(ix)))
    return df_out

df = bidirectional_different_SAB.to_data_frame()
df2 = to_fixedwidth(df)
# this uses the CSV writer to write the fixed width text file with a space as the separator
df2.to_csv("Display bidirectional relationships having different SAB.txt", sep=' ')
James
  • 32,991
  • 4
  • 47
  • 70
2

you can use display options to define how your df should be printed: https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html

see this post for some examples: How do I expand the output display to see more columns?

Raphael
  • 1,731
  • 2
  • 7
  • 23