我正在学习 pandas 和 Data Science 并且是初学者。我有如下数据
Rahul
1
2
5
Suresh
4
2
1
Dharm
1
3
4
我希望它在我的 dataframe 中
Rahul 1
2
5
Suresh 4
2
1
Dharm 1
3
4
我如何在不遍历每一行的情况下实现这一点,因为我有数十万的数据。我已经搜索了很多,但除了迭代之外找不到任何东西。有没有更好的办法。
谢谢你的好意和耐心
回答1
最好的格式取决于你打算用它做什么,但一个好的起点是这样做:
鉴于:
Rahul
1
2
5
Suresh
4
2
1
Dharm
1
3
4
正在做:
# Read in the file and call the column 'values':
df = pd.read_table(filepath, header=None, names=['values'])
# Create a new column with names filled in:
df['names'] = df['values'].replace('\d+', np.nan, regex=True).ffill()
# Drop the extra rows:
df = df[df['values'].str.isnumeric()].reset_index(drop=True)
print(df[['names', 'values']])
输出:
names values
0 Rahul 1
1 Rahul 2
2 Rahul 5
3 Suresh 4
4 Suresh 2
5 Suresh 1
6 Dharm 1
7 Dharm 3
8 Dharm 4