python pandas在使用后如何组合切片 [英] python pandas how to combine slices after using for
问题描述
我有一个数据框,有两列和5000行。
像:
AB
0 1 4
1 5 5
2 3 2
3 9 7
...
5000 8 3
I have a dataframe, with two columns and 5000 rows. like: A B 0 1 4 1 5 5 2 3 2 3 9 7 ... 5000 8 3
我想每100步分离一次数据框。所以我得到了50片。
为了进行培训,我接下来要做的是将这50个切片再次组合到一个新的数据帧或数组中,或者将我可以将数据输出到csv文件中的所有内容组合。
I want to separate the dataframe every 100 steps. So I get 50 slices. For a training, what's I want to do next is to combine the 50 slices again into a new dataframe or array or everything that I can output the data into csv file.
我使用以下命令将数据帧分为多个部分:
I used the command following to separate the dataframe into slices:
df_original=pd.read_csv('/data.csv')
df=pd.DataFrame(df_original, columns=['A','B'])
for i in range(0,len(df['A']),100):
df_100=df[i:i+100]
完成上述命令后,如何合并切片以进行下一步?
任何建议都会有所帮助。非常感谢。
After doing the command above, how can I combine the slices for next step? Any advice would be helpful. Thank you so much.
推荐答案
如果要有50个csv文件:
If you want have 50 csv files:
for i in range(0,len(df['A']),100):
df_100=df[i:i+100]
df_100.to_csv("file"+str(i)+".csv", index=False)
如果要对这些切片的数据帧进行一些处理,您可以将它们存储为字典:
If you want to do some process to those sliced dataframes, you can store them in as dictionary:
dict_of_df = {}
for i in range(0,len(df['A']),100):
dict_of_df["slice{}".format(i)]=df[i:i+100]
因此,您将通过 dict_of_df [key]
访问切片的数据帧,其中 key = slice0, slice100, slice200,...
So you will access to sliced dataframe by dict_of_df[key]
, where key = "slice0", "slice100", "slice200", ...
完成后那些切片的数据框并想要合并它们,
When you have done with those sliced dataframes and want to combine them,
df_final = pd.DataFrame()
for key, values in dict_of_df.items():
df_final = df_final.append(dict_of_df[key])
检查如果df_final排序不正确,则:
Check if df_final isn't sorted well, then:
df_final = df_final.sort_index()
然后导出回csv: df_final.to_csv( filename.csv)
这篇关于python pandas在使用后如何组合切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!