Home > Software engineering >  Drop column from tuple list in Pandas dataframe
Drop column from tuple list in Pandas dataframe

Time:07-15

This is my tuple I guess

df = [(1.8799187420058687, 1), (1.5963945918878317, 2)]

The type of df shows up as this

print(type(df))
<type 'list'>

My goal is to remove the second variable column displayed as 1 and 2

This is how I came up to the result (if that can help)

df = df.groupby('CBF_01').mean()
df = list(zip(df,df.index))

Expected output

df = (1.8799187420058687,1.5963945918878317)

CodePudding user response:

loop through each tuple, save the first value in a list and convert it to tuple.

new_df = tuple([x[0] for x in df])

output: (1.8799187420058687, 1.5963945918878317)

CodePudding user response:

Depending on how big the dataset is, you can loop through the dataframe and drop those, for example:

for i in range(len(df)-1):
    df['CBF_01'][i] = df['CBF_01'][i][0]
  • Related