简体   繁体   English

如何将此单列数据放入具有适当列的数据框中

[英]How to get this single column data into data frame with appropriate columns

I am learning pandas and Data Science and am a beginner.我正在学习熊猫和数据科学,并且是初学者。 I have a data as following我有如下数据

Rahul
1
2
5
Suresh
4
2
1
Dharm
1
3
4

I would like it in my dataframe as我希望它在我的数据框中

Rahul   1
        2
        5
Suresh  4
        2
        1
Dharm   1
        3
        4

How can I achieve this without iterating over every row, as I have data in hundreds of thousand.我如何在不遍历每一行的情况下实现这一点,因为我有数十万的数据。 I have searched a lot but cannot find anything other than iteration yet.我已经搜索了很多,但除了迭代之外找不到任何东西。 Is there a better way.有没有更好的办法。

Thank you for your kindness and patience谢谢你的好意和耐心

How it'd be best formatted depends on what you plan to do with it, but a good starting place would be doing this:最好的格式取决于你打算用它做什么,但一个好的起点是这样做:

Given:鉴于:

Rahul
1
2
5
Suresh
4
2
1
Dharm
1
3
4

Doing:正在做:

# 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']])

Output:输出:

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM