简体   繁体   中英

How to move specific data from one column to a new column on Pandas?

I have a set of data with 2 columns: Column1 = Hex Code and Column2= Current (A).

The data in Column1 is Hex Code, 27 different codes which repeats and for each Hex Code have Current (A) value on Column2.

I want to pick a set of 27 data points from Column1 & Column2 and place them into Coulmn3 & Column4.

Can someone help me to achieve this?

This is how the initial data looks

This is how i would like the data to be arranged

I am going tho show you my code. But I want to tell that you can not have repeating columns names. We suppose data is the name of your original dataset:

import pandas as pd

col_name1=data.columns.values[0]
col_name2=data.columns.values[1]

two_columns = data[[col_name1,col_name2]][0:27].values

two_columns = pd.DataFrame(two_columns,columns=[col_name1+'_1',col_name2+'_2'])

df = data.iloc[0:27,:]

df = df.join(two_columns)

print(df)

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.

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