Python 如何使用空值附加CSV文件数据
我是python和pandas数据集框架的新手。我正在尝试合并或附加两个csv文件数据。在第一步中,我能够读取CSV文件,但后来所需的输出并没有出现。我的CSV文件包含以下数据 physical.csv digital.csv 预期输出:Python 如何使用空值附加CSV文件数据,python,pandas,csv,Python,Pandas,Csv,我是python和pandas数据集框架的新手。我正在尝试合并或附加两个csv文件数据。在第一步中,我能够读取CSV文件,但后来所需的输出并没有出现。我的CSV文件包含以下数据 physical.csv digital.csv 预期输出: darn 3.001 0.421 0.532 darn null null null null ok 2.829 1.036 0.751 ok null null null null three 1.115 1.146 2.921 th
darn 3.001 0.421 0.532 darn null null null null
ok 2.829 1.036 0.751 ok null null null null
three 1.115 1.146 2.921 three null null null null
darn null null null darn,0.631,1.321,0.951,1.751
ok null null null ok,1.001,0.247,2.456,0.3216
three null null null three,0.285,1.283,0.924,956
import pandas as pd
a = pd.read_csv("D:/BMG/physical.csv")
physicalColumn = a.columns
print("physical.csv : ", a)
b = pd.read_csv("D:/BMG/digital.csv")
b = b.dropna(axis=1)
digitalColumn = b.columns
print("digital.csv : ", b)
appendColumns = physicalColumn.append(digitalColumn)
print("appendColumns : ", appendColumns)
merged = a.append(b)
m_col = ['title','stage','jan','feb','mar','apr','may','jun']
merged.columns = m_col
print("merge data : ", merged)
我的Python代码是:
darn 3.001 0.421 0.532 darn null null null null
ok 2.829 1.036 0.751 ok null null null null
three 1.115 1.146 2.921 three null null null null
darn null null null darn,0.631,1.321,0.951,1.751
ok null null null ok,1.001,0.247,2.456,0.3216
three null null null three,0.285,1.283,0.924,956
import pandas as pd
a = pd.read_csv("D:/BMG/physical.csv")
physicalColumn = a.columns
print("physical.csv : ", a)
b = pd.read_csv("D:/BMG/digital.csv")
b = b.dropna(axis=1)
digitalColumn = b.columns
print("digital.csv : ", b)
appendColumns = physicalColumn.append(digitalColumn)
print("appendColumns : ", appendColumns)
merged = a.append(b)
m_col = ['title','stage','jan','feb','mar','apr','may','jun']
merged.columns = m_col
print("merge data : ", merged)
我从代码中获得的输出:
('merge data : ', title stage jan feb mar apr may jun
0 NaN 0.532 0.421 NaN NaN NaN 3.001 darn
1 NaN 0.751 1.036 NaN NaN NaN 2.829 ok
2 NaN 2.921 1.146 NaN NaN NaN 1.115 three
0 1.321 NaN NaN 1.7510 0.631 0.951 NaN darn
1 0.247 NaN NaN 0.3216 1.001 2.456 NaN ok
2 1.283 NaN NaN 956.0000 0.285 0.924 NaN three)
- 函数执行沿一个轴执行连接操作的所有繁重工作,同时在其他轴上执行索引(如果有的话)的可选集合逻辑(并集或交集)
import pandas as pd
from io import StringIO
str1 = StringIO('''title,stage,jan,feb
darn,3.001,0.421,0.532
ok,2.829,1.036,0.751
three,1.115,1.146,2.921''')
a = pd.read_csv(str1,sep=',')
print("---a----")
print(a)
str2 = StringIO('''title,mar,apr,may,jun,
darn,0.631,1.321,0.951,1.751
ok,1.001,0.247,2.456,0.3216
three,0.285,1.283,0.924,956
''')
b = pd.read_csv(str2,sep=',')
b = b.dropna(axis=1)
print("---b---")
print(b)
print("-----output-------")
bigdata = pd.concat([a, b], ignore_index=True, sort =False)
print(bigdata)
O/p:
darn 3.001 0.421 0.532 darn null null null null
ok 2.829 1.036 0.751 ok null null null null
three 1.115 1.146 2.921 three null null null null
darn null null null darn,0.631,1.321,0.951,1.751
ok null null null ok,1.001,0.247,2.456,0.3216
three null null null three,0.285,1.283,0.924,956
import pandas as pd
a = pd.read_csv("D:/BMG/physical.csv")
physicalColumn = a.columns
print("physical.csv : ", a)
b = pd.read_csv("D:/BMG/digital.csv")
b = b.dropna(axis=1)
digitalColumn = b.columns
print("digital.csv : ", b)
appendColumns = physicalColumn.append(digitalColumn)
print("appendColumns : ", appendColumns)
merged = a.append(b)
m_col = ['title','stage','jan','feb','mar','apr','may','jun']
merged.columns = m_col
print("merge data : ", merged)
--a---
title stage jan feb
0 darn 3.001 0.421 0.532
1 ok 2.829 1.036 0.751
2 three 1.115 1.146 2.921
title mar apr may jun
0 darn 0.631 1.321 0.951 1.7510
1 ok 1.001 0.247 2.456 0.3216
2 three 0.285 1.283 0.924 956.0000
title stage jan feb mar apr may jun
0 darn 3.001 0.421 0.532 NaN NaN NaN NaN
1 ok 2.829 1.036 0.751 NaN NaN NaN NaN
2 three 1.115 1.146 2.921 NaN NaN NaN NaN
3 darn NaN NaN NaN 0.631 1.321 0.951 1.7510
4 ok NaN NaN NaN 1.001 0.247 2.456 0.3216
5 three NaN NaN NaN 0.285 1.283 0.924 956.0000
--b---
title stage jan feb
0 darn 3.001 0.421 0.532
1 ok 2.829 1.036 0.751
2 three 1.115 1.146 2.921
title mar apr may jun
0 darn 0.631 1.321 0.951 1.7510
1 ok 1.001 0.247 2.456 0.3216
2 three 0.285 1.283 0.924 956.0000
title stage jan feb mar apr may jun
0 darn 3.001 0.421 0.532 NaN NaN NaN NaN
1 ok 2.829 1.036 0.751 NaN NaN NaN NaN
2 three 1.115 1.146 2.921 NaN NaN NaN NaN
3 darn NaN NaN NaN 0.631 1.321 0.951 1.7510
4 ok NaN NaN NaN 1.001 0.247 2.456 0.3216
5 three NaN NaN NaN 0.285 1.283 0.924 956.0000
----输出---
title stage jan feb
0 darn 3.001 0.421 0.532
1 ok 2.829 1.036 0.751
2 three 1.115 1.146 2.921
title mar apr may jun
0 darn 0.631 1.321 0.951 1.7510
1 ok 1.001 0.247 2.456 0.3216
2 three 0.285 1.283 0.924 956.0000
title stage jan feb mar apr may jun
0 darn 3.001 0.421 0.532 NaN NaN NaN NaN
1 ok 2.829 1.036 0.751 NaN NaN NaN NaN
2 three 1.115 1.146 2.921 NaN NaN NaN NaN
3 darn NaN NaN NaN 0.631 1.321 0.951 1.7510
4 ok NaN NaN NaN 1.001 0.247 2.456 0.3216
5 three NaN NaN NaN 0.285 1.283 0.924 956.0000
IIUC,OP还需要两个名为
title
的列。合并[appendColumns]
?