0

If i have for instance, two numpy arrays np.full(10,1) and np.full(10,2), what is the most efficient way to create a pandas series?

Right now i would first create a pandas DataFrame and convert it into a pd.Series:

check = pd.DataFrame([np.full(10,1), np.full(10,2)])
check.transpose().unstack().reset_index(drop=True)
Out[0]: 
0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     1
8     1
9     1
10    2
11    2
12    2
13    2
14    2
15    2
16    2
17    2
18    2
19    2
dtype: int64

This seem extremely inefficient though, so what is the smartest way of doing this?

3
  • 1
    np.hstack can join those arrays into one. Commented Jan 23, 2022 at 16:12
  • Use a dictionary key and value where each value is a series then create your data frame using the dictionary Commented Jan 23, 2022 at 16:12
  • 1
    1. Merge data in numpy, 2. Directly construct Pandas Series and not DataFrame. pd.Series(np.hstack((np.full(10,1), np.full(10,2)))) Commented Jan 23, 2022 at 16:26

1 Answer 1

1

How about pd.Series(np.concatenate([arr1,arr2]))

Sign up to request clarification or add additional context in comments.

1 Comment

pd.Series(np.concatenate([arr1,arr2]))

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.