[英]How to transpose/separate list elements into panda data frame columns
I am trying to convert my output into a pandas data frame and I am struggling.我正在尝试将我的 output 转换为 pandas 数据框,但我正在苦苦挣扎。 I have this list which I obtain by reading multiple CSV files and reading a specific row and columns dfs = [(pd.read_csv(f,sep="\t",header = 1,engine='python').iloc[[6,7],[1]]) for f in files]
.我有这个列表,我通过读取多个 CSV 文件并读取特定的行和列dfs = [(pd.read_csv(f,sep="\t",header = 1,engine='python').iloc[[6,7],[1]]) for f in files]
。 each comma is separating the values i had gotten from one file.每个逗号分隔我从一个文件中获得的值。
[ 3.08945e+09
6 1825150.0
7 7746660.0,
3.14925e+09
6 2171520.0
7 6356880.0,
3.00826e+09
6 2344600.0
7 7881130.0,]
Screenshot of the code here这里的代码截图
How do I convert this into a three-column dataset?如何将其转换为三列数据集? like below.如下所示。
Any help would be appreciated.任何帮助,将不胜感激。
3.08945e+09 1825150.0 7746660.0
3.14925e+09 2171520.0 6356880.0
3.00826e+09 2344600.0 7881130.0
You can use pd.concat
.您可以使用pd.concat
。 If your list is k
:如果您的列表是k
:
>>> k
[ 3.08945e+09
6 1825150.0
7 7746660.0,
3.14925e+09
6 2171520.0
7 6356880.0,
3.00826e+09
6 2344600.0
7 7881130.0]
>>> pd.concat(k, axis=1).T.reset_index()
index 6 7
0 3.08945e+09 1825150.0 7746660.0
1 3.14925e+09 2171520.0 6356880.0
2 3.00826e+09 2344600.0 7881130.0
If you don't want the index, add drop=True
to reset_index
:如果您不想要索引,请将drop=True
添加到reset_index
:
>>> pd.concat(lst, axis=1).T
6 7
0 1825150.0 7746660.0
1 2171520.0 6356880.0
2 2344600.0 7881130.0
NOTE: Next time you ask a question, try to include the code as text, not image.注意:下次您提出问题时,请尝试将代码包含为文本,而不是图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.