Reputation: 9
My goal is to create a new Pandas Series object based on a set of user-defined inputs. I'm currently going about this task with the following code:
new_list = []
for _ in range(int(input("Enter number of entries: "))):
new_list.append(input("Enter an element for the list: "))
new_series = pd.Series(new_list)
print(new_series)
Works fine. However, I'm wondering if there's a way to create the new_series
Series object without appending and passing in new_list
Upvotes: 1
Views: 60
Reputation: 4725
Edit - I think I prefer the other answer, but have left here as an example of .append
You could append directly to a series object:
s = pd.Series()
for _ in range(int(input("Enter number of entries: "))):
s.append(pd.Series(input("Enter an element for the list: ")))
print(s)
eg:
In [14]: s = pd.Series()
In [15]: for _ in range(int(input('entries : '))):
...: s = s.append(pd.Series(input('el : ')))
...:
entries : 5
el : 3
el : 2
el : 1
el : 6
el : 7
In [16]: s
Out[16]:
0 3
0 2
0 1
0 6
0 7
dtype: object
Upvotes: 0
Reputation: 153500
IIUC, like this?
new_series = pd.Series(dtype='int')
for i in range(int(input("Enter number of entries: "))):
new_series.loc[i] = input("Enter an element for the list: ")
print(new_series)
Output:
Enter number of entries: 3
Enter an element for the list: 1
Enter an element for the list: 2
Enter an element for the list: 3
0 1
1 2
2 3
dtype: object
Upvotes: 1