Home > Mobile >  turn 2x16 dataframe into 4 x 4 matrix in python
turn 2x16 dataframe into 4 x 4 matrix in python

Time:09-17

I am trying to convert a file into an adjacency matrix. I need to do this in a way that allows files of different sizes to fill this matrix. My current working file is of size 4. This is my testing file, what I need is way of doing this in an abstract way to do larger files.

This is my test file. The 1 — 4 are the column that the Boolean values belong.

1,0
1,0
1,1
1,1
2,0
2,0
2,0
2,1
3,1
3,0
3,0
3,1
4,1
4,1
4,1
4,0

I would like an end result of:

0  0  1  1
0  0  0  1
1  0  0  1
1  1  1  0

Here is the code I have that produces a dataframe similar to my input file.

# Importing needed libraries
import os.path
from math import sqrt
import numpy as np
import pandas as pd

# changing filepath to a variable name
fileName = "./testAlgorithm.csv"

# opening file, doing file check, converting
# file to dataframe
if os.path.isfile(fileName):
    with open(fileName, "r") as csvfile:
        df = pd.read_csv(fileName, header=None)
else:
    print(f"file{fileName} does not exist")

# method used to count the number of lines
# in data file
def simpleCount(fileName):
    lines = 0
    for line in open(fileName):
        lines  = 1
    return sqrt(lines)

# method call for line count.
lineNum = simpleCount(fileName)
print(df)

num = int(simpleCount(fileName))

CodePudding user response:

you can try:

dummy = pd.DataFrame(columns= [['c1','c2','c3','c4']])
dummy['c1'] = np.array(df['c2'].loc[df['c1'] == 1])
dummy['c2'] = np.array(df['c2'].loc[df['c1'] == 2])
dummy['c3'] = np.array(df['c2'].loc[df['c1'] == 3])
dummy['c4'] = np.array(df['c2'].loc[df['c1'] == 4])

It will give you as :

  c1 c2 c3 c4
0  0  0  1  1
1  0  0  0  1
2  1  0  0  1
3  1  1  1  0

CodePudding user response:

df = pd.DataFrame({"A":[0,0,1,1,0,0,0,1,1,0,0,1,1,1,1,0]})
    
df.values.reshape(4,4)

If you want to make it back to a dataframe

pd.DataFrame(df.values.reshape(4,4), columns=["A", "B", "C", "D"])
  • Related