我正在尝试运行此代码:
import pandas as pd
import numpy as np
df = pd.read_csv('example.csv', sep=';', engine='python')
df1 =df.sort_values(['topic', 'student', 'level'], ascending=True)
count_list = df1.apply(lambda x: [df.ix[x.name-1].student if x.name >0 else np.nan, x.student, x.level>1], axis=1).values
#line giving the error
df1_count = pd.DataFrame(columns=['st_source','st_dest','reply_count'], data=count_list)
但我不断收到此错误消息:
ValueError: Shape of passed values is (1, 627), indices imply (3, 627)
有人知道我该怎么办吗?
谢谢!
答案 0 :(得分:2)
count_list = df1.apply(lambda x: (df.ix[x.name-1].student,np.nan,np.nan) if x.name 0 else (np.nan, x.student, x.level>1), axis=1).values
df2 = pd.DataFrame(count_list)
df2[['st_source','st_dest','reply_count']] = df2[0].apply(pd.Series)
df2 = df2.drop(0, 1)
这将返回一个像这样的DataFrame:
>>> df2
st_source st_dest reply_count
0 -0.689652 NaN NaN
1 0.696232 NaN NaN
2 0.767232 NaN NaN
3 NaN 0.696232 False
4 1.024604 NaN NaN
5 1.121045 NaN NaN
可能有一种更好,更有效的方法,但这解决了这个问题。注意我发出了if
语句,无论它落入哪个条件,都会返回长度为3的元组。