DataFrame — NameError: name 'M' is not defined

Question:

I got the error below:

NameError: name ‘M’ is not defined

I have 4 DataFrame variables X1, X2, X3, X4. Each DataFrame has a column of Time (ms).

I want to combine X1, X2, X3, and X4 to store them in a new DataFrame M with four columns A, B, C, and D.

I have imported all relevant libraries. Here are my codes.

M['A'] = pd.DataFrame(X1[' Time (ms) '])
M['B'] = pd.DataFrame(X2[' Time (ms) '])
M['C'] = pd.DataFrame(X3[' Time (ms) '])
M['D'] = pd.DataFrame(X4[' Time (ms) '])
Asked By: Tao

||

Answers:

you first need to create M. Try something like that:

M = pd.DataFrame({'A': X1[' Time (ms) '], 
                  'B': X2[' Time (ms) '],
                  'C': X3[' Time (ms) '], 
                  'D': X4[' Time (ms) ']})
Answered By: Lior T

Above answers will work fine, you can also create a dataframe first with only column names.

M = pd.DataFrame(columns=['A', 'B', 'C', 'D'])

And do what you were originally going for:

M['A'] = X1[' Time (ms) ']

M = int(input(2))
n = int(input(3))
for i in range(m):
row:[3]
for j in range(n):
row=[5]
for j in range(n):
row.append(int(input()))
matrix.append(row)

maaf kalo ga make sense

Answered By: ahfazzeen's
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.