python - Assigning multiple pandas columns using list slice -
in python, possible assign multiple variables objects in list. possible same pandas dataframe example:
df.trinucleotide 0 aca 1 acc 2 acg 3 act 4 aca 5 acc 6 acg 7 act 8 aca 9 acc df.loc[:,['first','second','third']] = df.trinucleotide.apply(lambda x: list(x))
this results in keyerror: "['first' 'second' 'third'] not in index" error while separating variables commas not work well.
df.loc[:,'first'], df.loc[:,'second'],df.loc[:,'third'] = df.trinucleotide.apply(lambda x: list(x)) valueerror: many values unpack
is there way without explicitly assigning each variable slice index of list?
to create multiple columns when using apply
, think it's best return series
rather list. can set column names setting them index series
. can do:
# reading data in df = pd.read_clipboard(header=none, index_col=0) df.columns = ['trinucleotide'] df out[9]: trinucleotide 0 0 aca 1 acc 2 acg # etc. new_cols = df['trinucleotide'].apply( lambda x: pd.series(list(x), index=['first', 'second', 'third']) ) df[['first', 'second', 'third']] = new_cols df out[8]: trinucleotide first second third 0 0 aca c 1 acc c c 2 acg c g 3 act c t 4 aca c 5 acc c c 6 acg c g 7 act c t 8 aca c 9 acc c c
Comments
Post a Comment