'How to solve (NaN error) when given column specific name
I have many text files include data as follow:
350.0 2.1021 0.0000 1.4769 0.0000
357.0 2.0970 0.0000 1.4758 0.0000
364.0 2.0920 0.0000 1.4747 0.0000
371.0 2.0874 0.0000 1.4737 0.0000
I need to give each column a specific name (Ex:a,b,c,d,e)
a b c d e
350.0 2.1021 0.0000 1.4769 0.0000
357.0 2.0970 0.0000 1.4758 0.0000
364.0 2.0920 0.0000 1.4747 0.0000
371.0 2.0874 0.0000 1.4737 0.0000
After that I will start to split columns and use them separately I wrote this code
import glob
import pandas as pd
input_files = glob.glob('input/*.txt')
for file_name in input_files:
data = pd.read_csv(file_name)
columns_list = ["a", "b", "c","d", "e"]
data_list = pd.DataFrame(data,columns=columns_list)
print(data_list)
the result is
a b c d e
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
Could you please help me?
Solution 1:[1]
You can define columns while reading from CSV file.
data = pd.read_csv(file_name, names=columns_list)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rafa? |