I would like to split a big dataframe into several smaller dataframes according to the value from the first column if that's possible, I didn't find it online. Example, I have this:
DF
Column1 Column2 Column3 Column4
A 1 2 1
A 1 1 2
A 3 2 2
B 2 1 2
B 3 1 1
split this into :
DF1
Column1 Column2 Column3 Column4
A 1 2 1
A 1 1 2
A 3 2 2
DF2
Column1 Column2 Column3 Column4
B 2 1 2
B 3 1 1
Simple as:
df1 = df[df['A'] == 'A']
df2 = df[df['B'] == 'B']
if you have unique values for the column you could create a list of dataframes:
df_lst = list()
unique_elements = df['column1'].unique()
for elm in unique_elements:
df_lst.append(df[df['column1'] == elm])
you can solve this by pandas and use the function groupby()
import pandas as pd
#load your file
df =pd.read_csv('sample.csv')
grouped = df.groupby(df.column1)
A = grouped.get_group("A")
B = grouped.get_group("B")
print(A)
print(B)
You can use groupby as:
df=[['A',1 ,1,2],['A',10,1,2],['B',10,1,2],['B',30,1,2]]
df = pd.DataFrame(df,columns=['a','b','c','d'])
d1,d2 = df.groupby('a')
print(d1[1])
print()
print(d2[1])
a b c d
0 A 1 1 2
1 A 10 1 2
a b c d
2 B 10 1 2
3 B 30 1 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.