Create new DF based on a Pandas column

0

I'm new to Python and Pandas I have a DF that with 3 columns, as in the example below:

SRC    Data1   Data2
AAA     180     122
BBB     168     121
CCC     165     147
DDD     140     156
EEE     152     103
AAA     170     100
CCC     166     112
DDD     116     155
EEE     179     119

What I need is to create a new DF for each value that is in SRC , for example:

DF_A

SRC    Data1   Data2
AAA    180     122
AAA    170     100

DF_B

SRC    Data1   Data2
BBB     168     121

and so on all values in SRC , what I did was create a DF with the unique values of SRC

pd.DataFrame(DataFrameBase.SRC.unique())

But I do not know if this will really help me!

Thanks for the help!

    
asked by anonymous 09.05.2017 / 22:50

1 answer

0

You can give the distinct in the SRC field with the command unique and then mount the dataFrames you need for example:

uniques = df.SRC.unique()
print(uniques)
dataFrames = []

for unique in uniques:
    data = df[df.SRC == unique]
    dataFrames.append(data)

print("Tamanho: " + str(len(dataFrames)) + '\n')

for data in dataFrames:
    print(data)
    print('\n')

After the distinct I run each item by mounting the corresponding set and add it to an array.

But if you just need% s of% s and% s of% s CRS you can do as follows:

df_a = df[df.SRC == 'AAA']
df_b = df[df.SRC == 'BBB']

print(df_a)
print('\n')
print(df_b)
    
10.05.2017 / 13:44