如何将成对共发表相关性CSV表转换为Neo4j/Cytoscape可导入的节点边表
Got it, let's break this down step by step—turning your 165x165 co-publication correlation CSV into node and edge tables for Neo4j or Cytoscape is totally doable, whether you prefer coding or no-code tools. First, let's confirm we're on the same page about your CSV structure: I'm assuming it's a square matrix where the first row and first column are author names, and each cross-cell is the correlation coefficient between two authors.
Both Neo4j and Cytoscape rely on two core files:
- Node Table: A list of unique authors (each author is a node)
- Edge Table: A list of pairwise relationships, with the correlation as an attribute
Method 1: Python (Pandas) – Fast & Flexible
This is the best approach for large tables like your 165x165 one, since it automates cleanup.
Generate Node Table
import pandas as pd # Load your correlation matrix CSV df = pd.read_csv("your_correlation_matrix.csv", index_col=0) # Extract unique author names for nodes nodes = pd.DataFrame({ "id": df.index.unique(), # Unique ID for each node "label": df.index.unique() # Display name (can match ID) }) # Save node table nodes.to_csv("authors_nodes.csv", index=False)
Pro tip: If you have extra author metadata (like affiliation), you can add those as additional columns here.
Generate Edge Table
We need to flatten the matrix into a list of source → target pairs, and clean up duplicates/self-correlations:
# Convert wide matrix to long format edges = df.stack().reset_index() edges.columns = ["source", "target", "correlation"] # Remove self-correlations (author paired with themselves) edges = edges[edges["source"] != edges["target"]] # Remove duplicate pairs (e.g., Alice→Bob and Bob→Alice) – keep only one direction edges = edges[edges["source"] < edges["target"]] # Save edge table edges.to_csv("co_author_edges.csv", index=False)
This gives you a clean list of unique co-publication relationships with their correlation values.
Method 2: Excel (No-Code)
If coding isn't your thing, Excel's Power Query can handle this:
Node Table
- Copy the first column of author names to a new worksheet
- Go to
Data → Remove Duplicatesto get a unique list of authors - Add headers
idandlabel(both can use the author name) - Save as
authors_nodes.csv
Edge Table
- Select your entire correlation matrix (including headers and first column)
- Go to
Data → From Table/Rangeto open Power Query - Select the author name column, then go to
Transform → Unpivot Columns → Unpivot Other Columns - Rename the columns:
Attribute→target,Value→correlation, and the first column →source - Filter out rows where
source = target(self-correlations) - To remove duplicate pairs, add a helper column with
=IF([@source]<[@target],1,0), then filter for rows with1 - Export the cleaned table as
co_author_edges.csv
Once you have your node and edge CSVs:
- Move both files to Neo4j's
importfolder (found in your database directory) - Run these Cypher queries in the Neo4j Browser:
// Import author nodes LOAD CSV WITH HEADERS FROM "file:///authors_nodes.csv" AS row CREATE (a:Author {id: row.id, name: row.label}); // Import co-publication edges with correlation attribute LOAD CSV WITH HEADERS FROM "file:///co_author_edges.csv" AS row MATCH (a:Author {id: row.source}), (b:Author {id: row.target}) CREATE (a)-[:CO_PUBLISHED_WITH {correlation: toFloat(row.correlation)}]->(b);
Note: toFloat() converts the correlation value from a string to a numeric type for calculations later.
Cytoscape makes this super straightforward:
- Open Cytoscape, then drag-and-drop both
authors_nodes.csvandco_author_edges.csvinto the window - Cytoscape will automatically detect:
idas the unique node identifiersourceandtargetas the endpoints of each edgecorrelationas an edge attribute
- You can then adjust styles (e.g., make edges thicker for higher correlation values) via the
Stylepanel on the right.
内容的提问来源于stack exchange,提问作者user3050520

