user8504877
user8504877

Reputation:

How to concat tuples and transform into dataframe

I search a way to concat my appended_data. At the moment my function gives back the last row which the for loop is running.

(4711, 'B')

But I need all data in one dataframe.

(4711, 'A')
(4711, 'B')

What do I have to change in my function, to get this result?

def test(df = None):
    loopset = np.array(df.col1.unique())

    appended_data = []
    for test in loopset:

        number = 4711

        appended_data = (number, test)
        print(appended_data)

    return appended_data 

finaler = test(df)
finaler

Upvotes: 1

Views: 62

Answers (2)

wwnde
wwnde

Reputation: 26676

What shape of df did you want? See my attempt

l=(4711, 'A')
j=(4711, 'B')
df=pd.DataFrame([list(l),list(j)], columns=['NUM','ALP'])

df

    NUM    ALP
0   4711    A
1   4711    B

Upvotes: 1

astrobot112
astrobot112

Reputation: 71

Change

    appended_data = (number, test)

to

    import pandas as pd
    ... 
    appended_data.append((number,test))
    appended_data=pd.DataFrame(appended_data)   

Upvotes: 0

Related Questions