简体   繁体   English

用 pandas 读取文件读取 csv 不起作用

[英]Reading file with pandas read csv is not working

All the column data is going inside the "index" column所有列数据都在“索引”列中

the header starts from row number 7 header 从第 7 行开始

'''
 index      mfg    legalId    resellerName   resellerCountry
(SONICWALL',' ','HEXAPAGE','FRANCE')                
(SONICWALL',' ','SEXTANT BTS LLC','UNITED STATES')              
(SONICWALL',' ','New Vision Networks, Inc.','UNITED STATES')
'''

All the values are inside index column, i want those values to come under specified column respectively Specified columns are所有值都在索引列内,我希望这些值分别位于指定列下 指定列是

mfg, legalId, resellerName, resellerCountry mfg、legalId、resellerName、resellerCountry

Below is the code which i have written, please help me how to do this下面是我写的代码,请帮助我如何做到这一点

df2=pd.read_csv(data, header=6, keep_default_na=False, sep=',', delimiter=',', quoting=csv.QUOTE_MINIMAL)

if your.csv file already has columns as first row then remove header=6 argument and let it infer which is default.如果 your.csv 文件已经将列作为第一行,则删除 header=6 参数并让它推断哪个是默认值。 在此处输入图像描述


If you keep "index" column in csv file then with header='infer' dataframe will look like below table which is not aligned with respect to data.如果您在 csv 文件中保留“索引”列,则使用 header='infer' dataframe 将如下表所示,该表未与数据对齐。 As data is shifted to left because data does not have index values mentioned由于数据没有提到索引值,因此数据向左移动

+----+-----------+-------+--------------------------+----------------+-------------------+
|    | index     | mfg   | legalId                  | resellerName   | resellerCountry   |
+====+===========+=======+==========================+================+===================+
|  0 | SONICWALL |       | HEXAPAGE                 | FRANCE         |                   |
+----+-----------+-------+--------------------------+----------------+-------------------+
|  1 | SONICWALL |       | SEXTANT BTS LLC          | UNITED STATES  |                   |
+----+-----------+-------+--------------------------+----------------+-------------------+
|  2 | SONICWALL |       | New Vision Networks Inc. | UNITED STATES  |                   |
+----+-----------+-------+--------------------------+----------------+-------------------+


you can remove "index" column from.csv file and reset_index on dataframe by:您可以通过以下方式从.csv 文件和 dataframe 上的 reset_index 删除“索引”列:

df2.reset_index(level=0, inplace=True)


and data will be:数据将是:

+----+---------+-----------+-----------+--------------------------+-------------------+
|    |   index | mfg       | legalId   | resellerName             | resellerCountry   |
+====+=========+===========+===========+==========================+===================+
|  0 |       0 | SONICWALL |           | HEXAPAGE                 | FRANCE            |
+----+---------+-----------+-----------+--------------------------+-------------------+
|  1 |       1 | SONICWALL |           | SEXTANT BTS LLC          | UNITED STATES     |
+----+---------+-----------+-----------+--------------------------+-------------------+
|  2 |       2 | SONICWALL |           | New Vision Networks Inc. | UNITED STATES     |
+----+---------+-----------+-----------+--------------------------+-------------------+

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

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