I imported some data. And its just one line
Jane
M
52,000
NYC
Mike
M
38,000
LA
AND so on I have 1000 lines
how do I get it to be
Name Sex salary City
Jane M 52,000 NYC
Mike M 38,000 LA
So every 5 lines make it a column I guess.
Thanks
Simply reshape
.
a = df['column_name'].to_numpy().reshape(-1, 4)
Notice that 4
above means 4
columns, which looks like is what you have after your edit. Before, it looked like you had 5
columns. Just adapt to whatever you have
To make it a DataFrame
pd.DataFrame(a, columns=['Name', 'Sex', 'salary', 'City'])
Since your question is not well defined and it changed a lot after your edit, I hope the above can help being a lead on what you need
You need to grab every subseries of 5 elements ( iloc[]
), transform them ( .T
) and concat
them.
data = pd.concat([df.iloc[s:s+4].reset_index(drop=True).T for s in range(0,len(df), 5)]).reset_index(drop=True)
data.columns = ['Name', 'Sex', 'City', 'Salary']
data['Sex'] = data['Sex'].str[0]
Output:
Name Sex City Salary
0 MiKE M NYC 52,000
1 MiKE M NYC 52,000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.