2

I want to create a new column with each item as an empty array in dataframe, for example, the dataframe is like:

-Index Name
-0 Mike
-1 Tom
-2 Lucy

I want to create a new column, make it like:

-Index Name1 Scores
-0 Mike []
-1 Tom []
-2 Lucy []

Because I need to append values in the arrays in the new column. How should I code?

2 Answers 2

5

Solution using just python:

import pandas as pd

df = pd.DataFrame({'Name': ['Mike', 'Tom', 'Lucy']})
df['Scores'] = [[]] * df.shape[0]
print(df)

Output:

   Name Scores
0  Mike     []
1   Tom     []
2  Lucy     []
Sign up to request clarification or add additional context in comments.

Comments

1

The solution using np.empty function:

import pandas as pd

df = pd.DataFrame({'Index': [0,1,2], 'Name': ['Mike', 'Tom', 'Lucy']})
df['Scores'] = pd.np.empty((len(df), 0)).tolist()
print(df)

The output:

   Index  Name Scores
0      0  Mike     []
1      1   Tom     []
2      2  Lucy     []

(len(df), 0) - tuple representing given shape of a new array

Comments

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.