简体   繁体   中英

Extract columns by file name from Pandas dataframe

I'm new to python and pandas.

I made a Pandas DataFrame for my data like the picture below. I want to extract x_c and y_c columns grouped by the file name. For example, I want to make the new dataframe, let's say df1, which has the values of x_c and y_c with the "file name" of "recon_image_0000.tif" Then I want to make another dataframe, df2 in the same manner but the file name of "recon_image_0001.tif". Like this way, I want to extract all the data and save separate till the last file name, "recon_image_0036.tif".

Can some one give me some tips to extract and make new pandas dataframe? 在此处输入图片说明

>>> df
  file_name  x_c  y_c
0     a.tif    7   37
1     a.tif   23   41
2     a.tif   98   21
3     b.tif   74  100
4     b.tif   84   78
5     b.tif   50   10
6     b.tif    1   10
7     c.tif   10   57
8     c.tif   49   15


>>> g = df.groupby("file_name")
>>> variables_names = [f"df_{e}" for e,i in enumerate(g, start=1)]
>>> for name, group in zip(variables_names, g):
        globals()[name] = group[1].reset_index(drop=True)
>>> df_1
  file_name  x_c  y_c
0     a.tif    7   37
1     a.tif   23   41
2     a.tif   98   21

>>> df_2
  file_name  x_c  y_c
0     b.tif   74  100
1     b.tif   84   78
2     b.tif   50   10
3     b.tif    1   10

>>> df_3
  file_name  x_c  y_c
0     c.tif   10   57
1     c.tif   49   15

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM