python - How to merge two columns together in Pandas -
i have data:
1975,a,b 1976,b,c 1977,b,a 1977,a,b 1978,c,d 1979,e,f 1980,a,f
i want have two-column list of years , items, so:
1975,a 1975,b ...
i have code:
import pandas # set column names colnames=['date','item1','item2'] # read csv adding column names data = pandas.read_csv('/users/simon/dropbox/work/datasets/lagtest.csv', names=colnames) # create dataframe info on dates first column datelist1 = data[['date', 'item1']] # create dataframe info on dates first column datelist2 = data[['date', 'item2']] bigdatelist = datelist1.append(datelist2) print bigdatelist
but gives me this:
date item1 item2 0 1975 nan 1 1976 b nan 2 1977 b nan 3 1977 nan 4 1978 c nan 5 1979 e nan 6 1980 nan 0 1975 nan b 1 1976 nan c 2 1977 nan 3 1977 nan b 4 1978 nan d 5 1979 nan f 6 1980 nan f
i want line numbering continuous , 2 last columns merged one. suggestions?
you looking pd.melt
.
suppose have dataframe
>>> df date item1 item2 0 1975 b 1 1976 b c 2 1977 b 3 1977 b 4 1978 c d 5 1979 e f 6 1980 f [7 rows x 3 columns]
now use this:
pd.melt(df, id_vars='year')['year','value']
to need.
>>> pd.melt(df, id_vars='date')[['date','value']] date value 0 1975 1 1976 b 2 1977 b 3 1977 4 1978 c 5 1979 e 6 1980 7 1975 b 8 1976 c 9 1977 10 1977 b 11 1978 d 12 1979 f 13 1980 f [14 rows x 2 columns]
Comments
Post a Comment