简体   繁体   English

将 pandas df 行拆分为多列

[英]Split pandas df rows into multiple columns

I have a csv that I needed to split on \n because because of the file type.我有一个 csv 由于文件类型的原因,我需要在 \n 上进行拆分。 After separating this df into two separate dfs I am left with rows that look like this将此 df 分成两个单独的 dfs 后,我剩下的行看起来像这样

27  Block\t"Column"\t"Row"\t"X"\t"Y"\t"Dia."\t"Fla...
28  1\t1\t1\t17834.00\t38902.00\t10.00\t0\t513.27\...
29  1\t2\t1\t17852.00\t38902.00\t10.00\t0\t495.84\...
30  1\t3\t1\t17870.00\t38902.00\t10.00\t0\t525.76\...
31  1\t4\t1\t17888.00\t38902.00\t10.00\t0\t456.27\...

How can I create columns from the delimiter \t?如何从分隔符 \t 创建列?

Something lke this might be what you're looking for...像这样的东西可能是你正在寻找的东西......

df['Block'] = df['Block'].str.split('/t').str[0]
df['Column'] = df['Columnl'].str.split('/t').str[1]

Use the following code:使用以下代码:

df[['Block','Column', 'Row', 'X', 'Y', etc]] = df['<Column Name>'].str.split(r'\t',expand=True)

Replace etc with the rest of the column names.etc替换为列名的 rest。 Replace <Column Name> with the name of the column when you print the df.打印 df 时将<Column Name>替换为列的名称。

A simpler method might be to download the dataframe as a csv, then reread it with a \t delimiter.更简单的方法可能是将 dataframe 下载为 csv,然后使用\t分隔符重新读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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