0

I have the following

x= np.arange(80,95,1)
y= np.arange(175,185,1)

I want to create pandas data frame containing x and y?

0

3 Answers 3

1

Use DataFrame.from_records to create a dataframe from different length arrays, and transpose the result if you want the arrays as columns:

pd.DataFrame.from_records([x,y]).T

     0      1
0   80.0  175.0
1   81.0  176.0
2   82.0  177.0
3   83.0  178.0
4   84.0  179.0
5   85.0  180.0
6   86.0  181.0
7   87.0  182.0
8   88.0  183.0
9   89.0  184.0
10  90.0    NaN
11  91.0    NaN
12  92.0    NaN
13  93.0    NaN
14  94.0    NaN
Sign up to request clarification or add additional context in comments.

Comments

0
import pandas as pd

df = pd.DataFrame(np.column_stack((x, y)), columns=["x", "y"])

There are probably other ways to do this, the DataFrame object can be constructed in a lot of different ways.

Edit: this actually won't work with different shapes of columns, but can be used with columns of the same length

Comments

0
import pandas as pd
df = pd.DataFrame({'x': x, 'y': y})

1 Comment

Can you explain yourself better please? I think my answer does provide an answer to OP's question. He has already defined x and y and with my code he can turn them into a DataFrame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.